简体   繁体   English

Reportlab - 如何在单词之间添加空格

[英]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之间添加空格:

word1    word2

How I can do this? 我怎么能这样做?

I know I am a little late on this but adding the html for non-breaking space &nbsp; 我知道我在这方面有点晚了,但是为不间断的空间添加了html &nbsp; worked for me. 为我工作。

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. 作为一种解决方法,您可以尝试在段落中添加空白(transperent或背景颜色)1px x 1px图像并将其缩放到所需的宽度。

<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). 另一个(繁琐)解决方案是使用canvas.beginText(x,y)创建的textobject来自己布局段落。

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. 希望这可以帮助。

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. 你可以用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM