Olga Donin
Olga Donin

Reputation: 69

Reportlab - How to add space between words

I have a text:

elements.append(Paragraph(<font size=10>word1 word2</font>, styleSheet["Normal"]))

I want to add space between word1 and word2:

word1    word2

How I can do this?

Upvotes: 5

Views: 7697

Answers (3)

Daniel Benjamin
Daniel Benjamin

Reputation: 11

def pad_text(text, num):
    text = str(text)
    num = int(num)
    text = text[:num]
    remaining = num - len(text)
    print(remaining)
    if (remaining == 0):
        return '<div>'+text+'</div>'
    else:
        res = '<div>'+text+ ' &nbsp;'*remaining +'</div>'
        return res

You can use this.

Upvotes: 1

Don Guernsey
Don Guernsey

Reputation: 137

I know I am a little late on this but adding the html for non-breaking space &nbsp; worked for me.

Upvotes: 11

Denis
Denis

Reputation: 570

I doubt there is an easy solution for it.

As a workaround you could try adding a blank (transperent or background color) 1px x 1px image in your paragraph and scale it to the desired width.

<font size=10>word1<img src="../path/to/image" width="10" />word2</font>

Another (tedious) solution would be to layout your paragraph yourself with textobjects created by canvas.beginText(x, y).

textobject = canvas.beginText(x, y)
textobject.setWordSpace(10)
textobject.textLine("word1 word2")
... (setting other parameters such as font etc.)
canvas.drawText(textobject)

Hope this helps.

Upvotes: 3

Related Questions