在 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')