Python Reportlab多行

时间:2015-05-18 14:52:54

标签: python pdf subprocess reportlab

我正在尝试将我的磁盘状态写入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)

1 个答案:

答案 0 :(得分:1)

您没有循环遍历数据中的,而是遍历字符。例如:

>>> 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 your来生成此代码:

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