2015-05-18 49 views
1

我正在嘗試將我的磁盤狀態寫入pdf。問題在於它沒有寫多行文字:每個字母的文字都是垂直的。Python Reportlab多行

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) 

回答

2

你是不是循環在的數據,但在字符。例如:

>>> 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`環路,產生這樣的:

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