简体   繁体   English

Python Reportlab多行

[英]Python Reportlab multiple lines

I am trying to write my disk status to a pdf. 我正在尝试将磁盘状态写入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: 因此,在您的代码中,将.split('\\n') to your for`循环中以生成此代码:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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