在 reportlab 中将宽度设置为 textobject
setting width to textobject in 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.drawText(t)
此代码适用于普通字符串,但当一些大写字母出现在文本中时,其宽度会增加并超出页面范围。
那么,我可以根据页面宽度而不是根据数字字符来设置文本宽度吗?
使用 reportlab.platypus.Paragraph
而不是绘制到 canvas,您将免费获得文字换行。
您可以将 Paragraph
与 HTML 中的 <div>
进行比较:Paragraph
的宽度为(文档的)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)
我正在使用 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.drawText(t)
此代码适用于普通字符串,但当一些大写字母出现在文本中时,其宽度会增加并超出页面范围。 那么,我可以根据页面宽度而不是根据数字字符来设置文本宽度吗?
使用 reportlab.platypus.Paragraph
而不是绘制到 canvas,您将免费获得文字换行。
您可以将 Paragraph
与 HTML 中的 <div>
进行比较:Paragraph
的宽度为(文档的)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)