gpt4 book ai didi

python - Reportlab new line in a long line

转载 作者:行者123 更新时间:2023-11-28 21:13:25 33 4
gpt4 key购买 nike

enter image description here

我需要一个新行,这样我就可以在 PFD 中看到一个格式,我尝试添加一个页面宽度但它没有用,我用另一个东西/n 也没有用。这是我的代码。我可以手动添加格式,因为我需要显示从数据库中获取的信息,并且我在一长行中获取信息。

def PdfReportView(request):
print id
# Create the HttpResponse object with the appropriate PDF headers.
response = HttpResponse(content_type='application/pdf')
response['Content-Disposition'] = 'filename="PatientReport.pdf"'
c = canvas.Canvas(response, pagesize=letter)
t = c.beginText()
t.setFont('Helvetica-Bold', 10)
t.setCharSpace(3)
t.setTextOrigin(50, 700)
t.textLine("Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.")
c.drawText(t)
c.showPage()
c.save()
return response

最佳答案

如果您的文本输入中有 \n,您可以使用 textLines():

t.textLines('''Lorem Ipsum is simply dummy text of the printing and 
typesetting industry. Lorem Ipsum has been the industry's standard dummy text
ever since the 1500s, when an unknown printer took a galley of type and
scrambled it to make a type specimen book.''')

如果您的文本是一行,您可以使用 textwrap 模块将其分成多行:

from textwrap import wrap

text = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book."

wraped_text = "\n".join(wrap(text, 80)) # 80 is line width

t.textLines(wraped_text)

关于python - Reportlab new line in a long line,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32830274/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com