在Python中创建和写入pdf文件
问题描述:
为什么不行:
with open('file.pdf', 'w') as outfile:
outfile.write("Hello")
该代码可以正常工作,但是无法打开.pdf文件.普通文本文件和pdf有什么区别? 如果我想在python中创建并写入pdf文件怎么办?
The code works fine, but the .pdf file cannot be opened. What's the difference between a normal text file and pdf? What to do if I want to create and write to a pdf file in python?
答
您可以安装fpdf库,然后:
you could install the fpdf library and then:
from fpdf import FPDF
pdf = FPDF()
pdf.add_page()
pdf.set_xy(0, 0)
pdf.set_font('arial', 'B', 13.0)
pdf.cell(ln=0, h=5.0, align='L', w=0, txt="Hello", border=0)
pdf.output('test.pdf', 'F')