Python 字符串换行

Python String Wrap

我想在此脚本中将字符串换行到 30,700。这样做的最佳方法是什么,我尝试过使用 textWrap 但它似乎不起作用。这是我的代码:

from reportlab.lib.pagesizes import letter
from reportlab.pdfgen import canvas

canvas = canvas.Canvas("Forensic Report.pdf", pagesize=letter)
canvas.setLineWidth(.3)
canvas.setFont('Helvetica', 12)

canvas.drawString(30,750,'LYIT MOBILE FORENSICS DIVISION')
canvas.drawString(500,750,"Date: 12/02/2018")
canvas.line(500,747,595,747)

canvas.drawString(500,725,'Case Number:')
canvas.drawString(580,725,"10")
canvas.line(500,723,595,723)


canvas.drawString(30, 700, 'This forensic report has been compiled by the forensic examiner in conclusion to the investigation into the RTA case which occured on 23/01/2018')


canvas.save()
print("Forensic Report Generated")

也许您想使用 drawText? 这样做,您的代码将是

from reportlab.lib.pagesizes import letter
from reportlab.pdfgen import canvas

canvas = canvas.Canvas("Forensic Report.pdf", pagesize=letter)
canvas.setLineWidth(.3)
canvas.setFont('Helvetica', 12)
canvas.drawString(30,750,'LYIT MOBILE FORENSICS DIVISION')
canvas.drawString(500,750,"Date: 12/02/2018")
canvas.line(500,747,595,747)

canvas.drawString(500,725,'Case Number:')
canvas.drawString(580,725,"10")
canvas.line(500,723,595,723)

line1 = 'This forensic report has been compiled by the forensic'
line2 = 'examiner in conclusion to the investigation into the RTA'
line3 = 'case which occured on 23/01/2018'
textobject = canvas.beginText(30, 700)
lines = [line1, line2, line3]
for line in lines:
    textobject.textLine(line)

canvas.drawText(textobject)
canvas.save()

这也是建议的解决方案 here。不幸的是,我不认为它是在新行中自动换行的有效解决方案,即您应该自己管理如何拆分字符串。

您应该使用函数包装字符串本身并绘制 returned 的内容。要获得正确包装的字符串,您可以像这样指定行长度:

def wrap(string, length):
    if len(string) < length:
        return string
    return string[:length:] + '\n' + wrap(string[length::], length)

wrap 函数首先检查字符串长度是否小于指定长度,如果是,则立即 return 进行处理。如果它更长,那么它会在子字符串的末尾附加一个换行符,直到达到 length,然后将字符串的其余部分发送到 wrap() 函数以检查其余部分。

运行:

string = "This is a really super duper long string from some forensic report and should take up a lot of space..."
length = 20
print(wrap(string, length))

将打印出:

This is a really sup
er duper long string
 from some forensic 
report and should ta
ke up a lot of space
...

因为您可能不希望每一行都在行宽处被截断,我们可以通过添加另一个递归函数来检查最近的白色 space 字符来解决这个问题,如下所示:

def seek(string, index):
    if string[index-1] == ' ':
        return index
    return seek(string, index-1)

seek() 将 return 任何字符串(在本例中为子字符串)的最后一个白色 space 字符的索引。

注意:seek() 必须检查前一个字符 string[index-1] 否则将为您提供 space 字符的索引并且 wrap() 会将其附加到每个新行.

然后您可以像这样修改 wrap() 函数:

def wrap(string, length):
    if len(string) < length:
        return string
    pos = seek(string, length)
    return string[:pos:] + '\n' + wrap(string[pos::], length)

运行:

string = "This is a really super duper long string from some forensic report and should take up a lot of space..."
length = 20
print(wrap(string, length))

打印出来:

This is a really 
super duper long 
string from some 
forensic report and 
should take up a 
lot of space...