Reputation:
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)
Upvotes: 1
Views: 2300
Reputation: 4395
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'):
Upvotes: 1