gpt4 book ai didi

Python Reportlab 多行

转载 作者:太空宇宙 更新时间:2023-11-03 17:42:33 25 4
gpt4 key购买 nike

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

最佳答案

您不是在数据中的上循环,而是在字符上循环。例如:

>>> 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') 添加到 for` 循环中以生成以下内容:

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

关于Python Reportlab 多行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30306446/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com