python 生成 PDF
PDF generation in python
我正在尝试为我创建的程序生成 PDF 文件而不是文本文件。
我的问题是我看了一个报告实验室,它似乎对我需要的东西来说过于复杂,因为我想要的只是本质上 print
到 pdf 而不是文本文件。
当前的测试代码,它可以工作,但我对定位和页面末尾的所有行 运行 感到困惑,有人可以告诉我定位如何与 reportlab
一起工作吗
from reportlab.pdfgen import canvas
def genText():
text =["Call me Ishmael.",
"Some years ago- never mind how long precisely- having little or no money in my purse,",
"and nothing particular to interest me on shore,",
"I thought I would sail about a little and see the watery part of the world."]
return text
def testText(page,text):
from reportlab.lib.units import inch
textobject = page.beginText()
textobject.setTextOrigin(inch, 2.5*inch)
textobject.setFont("Helvetica-Oblique", 14)
for line in text:
textobject.textLine(line)
page.drawText(textobject)
page = canvas.Canvas("JIMTEST.pdf")
text = genText()
testText(page, text)
page.showPage()
page.save()
我的程序实际输出的数据:
---------------------------------------------------------
Milk Company: Bobbys Milk
Haulier: Jims Hauliers
Truck: T55JHH
Driver: 123 Route: 852
Joe Bloggs
Everyday Collection
MilkType: Ordinary
---------------------------------------------------------
Last TankWash
Start Time: 2016/03/31 13:30:32
Finsished: 14:21:03
Litres: 9451
Temperature: 70.0 deg_C
770500 CREAMERY
---------------------------------------------------------
Locn Litres
770083 Wyrill
Coll 1643 2.0 deg_C smp 143
2016/04/01 06:40:28
770084 Foster
Coll 2242 1.0 deg_C smp 28
2016/04/01 07:17:57
770080 Dugdale
Coll 8237 4.0 deg_C smp 49
2016/04/01 08:02:39
770086 Cragg
Coll 4591 1.7 deg_C smp 68
2016/04/01 09:00:17
770051 D & S Spence
Coll 2868 3.7 deg_C smp 83
2016/04/01 10:06:11
770500 CREAMERY
delyFZ -19581
Tank# 0 Ower# 3805
2016/04/01 11:14:11
---------------------------------------------------------
Milk Collected: 19581
Milk OnBoard: 0
---------------------------------------------------------
Estimated Print Time at: 2016/04/01 11:14:16
谢谢
我建议你看看 reportlab 的鸭嘴兽部分。
段落的作用很像 HTML 中的 DIV 容器,会在文档末尾自动换行。
from reportlab.lib.pagesizes import A4
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
def genText():
text = ["Call me Ishmael.",
"Some years ago- never mind how long precisely- having little or no money in my purse,",
"and nothing particular to interest me on shore,",
"I thought I would sail about a little and see the watery part of the world."]
return text
styles = getSampleStyleSheet()
doc = SimpleDocTemplate("my_doc.pdf", pagesize=A4)
Story=[]
text = genText()
for t in text:
Story.append(Paragraph(t, styles["Normal"]))
Story.append(Spacer(10, 10))
t = "lorem ipsum " * 100
Story.append(Paragraph(t, styles["Normal"]))
doc.build(Story)
当然,您也可以混合使用 canvas 和鸭嘴兽 - 这只是一个简单的示例。
为了定位你的元素,我建议你看看鸭嘴兽 Table
。
您可以查看 pdfme 图书馆。它是 python 中创建 PDF 文档最强大的库,而且非常易于使用。
要将这些日志“打印”到 PDF 文档中,您可以这样做:
from pdfme import build_pdf
with open("logs.pdf", 'wb') as f:
build_pdf({
"style": {"s": 12, "text_aling": "j"},
"sections": [
{"content": [LOGS_VARIABLE_STR]}
]
}, f)
就是这样。您还可以拆分字符串,如果需要,可以给它一些格式,然后按照文档中的说明添加格式化的段落 (https://pdfme.readthedocs.io/)
我正在尝试为我创建的程序生成 PDF 文件而不是文本文件。
我的问题是我看了一个报告实验室,它似乎对我需要的东西来说过于复杂,因为我想要的只是本质上 print
到 pdf 而不是文本文件。
当前的测试代码,它可以工作,但我对定位和页面末尾的所有行 运行 感到困惑,有人可以告诉我定位如何与 reportlab
一起工作吗from reportlab.pdfgen import canvas
def genText():
text =["Call me Ishmael.",
"Some years ago- never mind how long precisely- having little or no money in my purse,",
"and nothing particular to interest me on shore,",
"I thought I would sail about a little and see the watery part of the world."]
return text
def testText(page,text):
from reportlab.lib.units import inch
textobject = page.beginText()
textobject.setTextOrigin(inch, 2.5*inch)
textobject.setFont("Helvetica-Oblique", 14)
for line in text:
textobject.textLine(line)
page.drawText(textobject)
page = canvas.Canvas("JIMTEST.pdf")
text = genText()
testText(page, text)
page.showPage()
page.save()
我的程序实际输出的数据:
---------------------------------------------------------
Milk Company: Bobbys Milk
Haulier: Jims Hauliers
Truck: T55JHH
Driver: 123 Route: 852
Joe Bloggs
Everyday Collection
MilkType: Ordinary
---------------------------------------------------------
Last TankWash
Start Time: 2016/03/31 13:30:32
Finsished: 14:21:03
Litres: 9451
Temperature: 70.0 deg_C
770500 CREAMERY
---------------------------------------------------------
Locn Litres
770083 Wyrill
Coll 1643 2.0 deg_C smp 143
2016/04/01 06:40:28
770084 Foster
Coll 2242 1.0 deg_C smp 28
2016/04/01 07:17:57
770080 Dugdale
Coll 8237 4.0 deg_C smp 49
2016/04/01 08:02:39
770086 Cragg
Coll 4591 1.7 deg_C smp 68
2016/04/01 09:00:17
770051 D & S Spence
Coll 2868 3.7 deg_C smp 83
2016/04/01 10:06:11
770500 CREAMERY
delyFZ -19581
Tank# 0 Ower# 3805
2016/04/01 11:14:11
---------------------------------------------------------
Milk Collected: 19581
Milk OnBoard: 0
---------------------------------------------------------
Estimated Print Time at: 2016/04/01 11:14:16
谢谢
我建议你看看 reportlab 的鸭嘴兽部分。
段落的作用很像 HTML 中的 DIV 容器,会在文档末尾自动换行。
from reportlab.lib.pagesizes import A4
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
def genText():
text = ["Call me Ishmael.",
"Some years ago- never mind how long precisely- having little or no money in my purse,",
"and nothing particular to interest me on shore,",
"I thought I would sail about a little and see the watery part of the world."]
return text
styles = getSampleStyleSheet()
doc = SimpleDocTemplate("my_doc.pdf", pagesize=A4)
Story=[]
text = genText()
for t in text:
Story.append(Paragraph(t, styles["Normal"]))
Story.append(Spacer(10, 10))
t = "lorem ipsum " * 100
Story.append(Paragraph(t, styles["Normal"]))
doc.build(Story)
当然,您也可以混合使用 canvas 和鸭嘴兽 - 这只是一个简单的示例。
为了定位你的元素,我建议你看看鸭嘴兽 Table
。
您可以查看 pdfme 图书馆。它是 python 中创建 PDF 文档最强大的库,而且非常易于使用。
要将这些日志“打印”到 PDF 文档中,您可以这样做:
from pdfme import build_pdf
with open("logs.pdf", 'wb') as f:
build_pdf({
"style": {"s": 12, "text_aling": "j"},
"sections": [
{"content": [LOGS_VARIABLE_STR]}
]
}, f)
就是这样。您还可以拆分字符串,如果需要,可以给它一些格式,然后按照文档中的说明添加格式化的段落 (https://pdfme.readthedocs.io/)