Python Reportlab 多行

Python Reportlab multiple lines

我正在尝试将我的磁盘状态写入 pdf。问题是它无法写入多行:每个字母的文本都是垂直的。

import subprocess
from reportlab.pdfgen import canvas

p = subprocess.Popen('df -h', stdout=subprocess.PIPE, shell=True)
(disk, err) = p.communicate()
print disk

def hello(disk):
            height= 700
            c = canvas.Canvas("diskreport.pdf")
            c.drawString(200,800,"Diskreport")
            for line in disk:
                    c.drawString(100,height,line.strip()) 
                    height = height - 25
            c.showPage()
            c.save()
hello(disk)

您不是在数据中的 上循环,而是在 个字符 上循环。例如:

>>> data="""a
... b
... line 3"""
>>> # this will print each character (as in your code)
... for line in data: print line
... 
a


b


l
i
n
e

3
>>> 
>>> # split into lines instead
... for line in data.split('\n'): print line
... 
a
b
line 3
>>>

因此在您的代码中添加 .split('\n') to yourfor`-loop 以生成此代码:

for line in disk.split('\n'):