简体   繁体   中英

Python Reportlab multiple lines

I am trying to write my disk status to a pdf. The problem is it's failing in writing multiple lines: the text for each letter goes vertically.

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)

You are not looping over the lines in the data but over the characters . Ex:

>>> 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
>>>

So in your code you add .split('\\n') to your for`-loop to produce this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM