在 Python 中创建和写入 pdf 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45953770/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 17:22:18  来源:igfitidea点击:

Creating and writing to a pdf file in Python

pythonfilepdf

提问by Rasmus Lyngdal-Christensen

Why won't this work :

为什么这行不通:

with open('file.pdf', 'w') as outfile:
    outfile.write("Hello")

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?

代码工作正常,但无法打开 .pdf 文件。普通文本文件和 pdf 有什么区别?如果我想在 python 中创建和写入 pdf 文件怎么办?

回答by Rasmus Lyngdal-Christensen

you could install the fpdf library and then:

您可以安装 fpdf 库,然后:

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

回答by gsamaras

What's the difference between a normal text file and pdf?

普通文本文件和 pdf 有什么区别?

A PDF file has a specific format. You can read more here. A text is a much simpler file, thus when you attempt to open a file that youthink it's a PDF, but doesn't have this format, the file cannot be opened.

PDF 文件具有特定格式。您可以在此处阅读更多内容。文本文件是一个简单得多的文件,因此当您尝试打开一个认为它是 PDF 的文件,但没有这种格式时,该文件将无法打开。

What to do if I want to create and write to a pdf file in python?

如果我想在 python 中创建和写入 pdf 文件怎么办?

You need to use a module, like PyPDF2, Reportlabor FPDF. Moreover read Python PDF library.

您需要使用一个模块,例如PyPDF2ReportlabFPDF。此外阅读Python PDF library

回答by Matthias Fripp

Every type of file has its own internal format - a set of rules about what has to go where to define the information it is meant to represent. Usually the file extension ('.pdf' in this case) is set to tell you what internal format a file uses, but there's no absolute guarantee of that.

每种类型的文件都有其自己的内部格式 - 一组关于必须去哪里定义它要表示的信息的规则。通常文件扩展名(在这种情况下为“.pdf”)设置为告诉您文件使用什么内部格式,但并没有绝对的保证。

If you write a string to a file with Python, the file will have exactly what you put in it, in this case just the five ASCII characters H, e, l, l and o. That would correspond to the normal format for a text file. So in this case, you have created a text file but put a '.pdf' extension on it. Its internal format is still a text file, and if you rename it to 'file.txt', you'll find you can open it just fine (with a text editor).

如果您使用 Python 将字符串写入文件,则该文件将包含您放入其中的内容,在这种情况下,只有五个 ASCII 字符 H、e、l、l 和 o。这将对应于文本文件的正常格式。因此,在本例中,您创建了一个文本文件,但在其上添加了“.pdf”扩展名。它的内部格式仍然是一个文本文件,如果你将它重命名为'file.txt',你会发现你可以很好地打开它(用文本编辑器)。

If you want to create a true PDF file (something with the correct internal format for a PDF), you would need to use a specialized package that can write that type of file. @gsamaras and @rasmus-lyngdal-christensen gave some good suggestions (Reportlab, PyPDF2 and fpdf).

如果要创建真正的 PDF 文件(具有正确的 PDF 内部格式的文件),则需要使用可以编写该类型文件的专用包。@gsamaras 和 @rasmus-lyngdal-christensen 给出了一些很好的建议(Reportlab、PyPDF2 和 fpdf)。