在 Python 中创建和写入 pdf 文件 [英] Creating and writing to a pdf file in Python

查看:35
本文介绍了在 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')

这篇关于在 Python 中创建和写入 pdf 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆