Python 在reportlab中设置textobject的宽度
我正在使用reportlab创建报告。下面是在报告中插入文本的代码Python 在reportlab中设置textobject的宽度,python,reportlab,Python,Reportlab,我正在使用reportlab创建报告。下面是在报告中插入文本的代码 c = canvas.Canvas("reports/abc.pdf", pagesize=A4) t = c.beginText() t.setFont('Times-Roman', 14) t.setTextOrigin(0.3*inch,6.5*inch) wraped_text = "\n".join(wrap(Desc,100)) # 100 is line width t.textLines(wraped_text
c = canvas.Canvas("reports/abc.pdf", pagesize=A4)
t = c.beginText()
t.setFont('Times-Roman', 14)
t.setTextOrigin(0.3*inch,6.5*inch)
wraped_text = "\n".join(wrap(Desc,100)) # 100 is line width
t.textLines(wraped_text)
c.drawText(t)
此代码适用于普通字符串,但当一些大写字母出现在文本中时,其宽度会增加,并超出页面。
因此,我可以根据页面宽度而不是字符数来设置文本宽度吗?使用
reportlab.platypus.paragration
而不是绘制到画布上,您将免费获得文本包装
您可以将段落
与HTML中的
进行比较:段落
的宽度为100%(文档的宽度),高度取决于内容
from reportlab.lib.pagesizes import A4
from reportlab.platypus import SimpleDocTemplate, Paragraph
from reportlab.lib.styles import getSampleStyleSheet
very_long_text = "Your very long text here ..."
styles = getSampleStyleSheet()
doc = SimpleDocTemplate("my_doc.pdf", pagesize=A4)
Story=[]
Story.append(Paragraph(very_long_text, styles["Normal"]))
doc.build(Story)
我要试试这个。我还有其他内容,如小字符串和图像要添加到报告中,我可以使用此方法添加这些内容吗?platypus中也有一个图像类,具有不同的设置(拉伸、缩放等)-只需阅读文档;)