
PDF 文件格式通常用于呈现和交换文档。此外,它还提供了许多好处,例如减少 MB 和呈现文本、图像、表格、注释、书签和超链接,而不会丢失布局和格式。 Python 开发人员可能会考虑这些优势,并希望将他们的数据发布为 PDF。如果您是这些开发人员中的一员,本文将帮助您学习如何使用 Python 创建 PDF 文档。
本文将涵盖以下几点:
安装 Python PDF Creator 包
要以编程方式创建 PDF 文档,我们将使用 Python PDF creator 包,它允许开发人员在不使用 Adobe Acrobat 的情况下生成、读取、转换和操作 PDF 文件。
请下载 或使用下面给出的 pip 命令从 PyPI 安装软件包:
pip install aspose-pdf
在 Python 中创建 PDF 文档
首先,我们将解释如何创建包含文本片段的 PDF 文档。以下是从头开始创建简单 PDF 文档的步骤。
- 创建一个 Document 类的实例。
- 添加一个新的 Page 到文档的页面集合。
- 创建新的 TextFragment 并将其添加到 PDF 的段落中。
- 使用 Document.Save() 方法生成 PDF 文件。
以下代码片段演示了如何在 Python 中创建 PDF 文档。
# Initialize document object | |
document = aspose.pdf.Document() | |
# Add page | |
page = document.pages.add() | |
# Initialize textfragment object | |
text= "Lorem ipsum dolor sit amet, consectetur adipiscing elit, " \ | |
"sed do eiusmod tempor incididunt ut labore et dolore magna aliqua." \ | |
" Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris" \ | |
" nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in " \ | |
"reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur." \ | |
" Excepteur sint occaecat cupidatat non proident, sunt in culpa qui " \ | |
"officia deserunt mollit anim id est laborum." | |
text_fragment = aspose.pdf.text.TextFragment (text) | |
# Add text fragment to new page | |
page.paragraphs.add(text_fragment) | |
# Save updated PDF | |
document.save("output.pdf") |
您将看到如下输出。

使用 Python 在 PDF 中应用文本格式
创建文档后,让我们学习如何使用 Python PDF 库在 PDF 中应用文本格式。请按照以下步骤操作。
- 使用 Document 类创建一个新的 PDF 文档。
- 选择要放置文本的页面。
- 创建 TextFragment 的对象并设置它们的文本和其他格式选项,如位置、字体、颜色、大小等。
- 使用 Page.paragraphs.add() 方法将文本片段添加到页面。
- 调用 Document.Save() 方法创建 PDF 文档。
以下代码片段显示了如何使用 Python 以编程方式在 PDF 中应用文本格式。
# Initialize document object | |
document = aspose.pdf.Document() | |
# Add page | |
page = document.pages.add() | |
# Create text fragments | |
text1= "Text1: Lorem ipsum dolor sit amet, consectetur adipiscing elit, " \ | |
"sed do eiusmod tempor incididunt ut labore et dolore magna aliqua." \ | |
" Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris" \ | |
" nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor" | |
text2= "Text2: Lorem ipsum dolor sit amet, consectetur adipiscing elit, " \ | |
"sed do eiusmod tempor incididunt ut labore et dolore magna aliqua." | |
text_fragment1 = aspose.pdf.text.TextFragment (text1) | |
text_fragment1.position= aspose.pdf.text.Position(100, 700) | |
text_fragment2 = aspose.pdf.text.TextFragment (text2) | |
text_fragment2.position= aspose.pdf.text.Position(100, 600) | |
# Set text properties | |
text_fragment1.text_state.font_size = 12; | |
text_fragment1.text_state.font = aspose.pdf.text.FontRepository.find_font("TimesNewRoman"); | |
text_fragment1.text_state.background_color=aspose.pdf.Color().aqua | |
text_fragment2.text_state.underline = True | |
text_fragment2.text_state.strike_out = True | |
# Add text fragments to page | |
page.paragraphs.add(text_fragment1) | |
page.paragraphs.add(text_fragment2) | |
# Save updated PDF | |
document.save("output.pdf") |
您将看到输出的 PDF,如下所示。

Python:以编程方式在 PDF 文件中插入图像
我们在上一节中学习了更改文本格式。因此,在本节中,我们将说明如何将图像添加到 PDF 文档。请按照以下步骤完成这项工作。
- 使用 Document 类创建一个新的 PDF 文档。
- 获取要插入图像的所需页面。
- 使用 Page.add_image(file_path, rectangle) 将图像添加到页面,而 Rectangle 类用于将图像放置在页面上。
- 使用 Document.Save() 方法生成 PDF 文档。
以下代码示例展示了如何使用 Python 在 PDF 中添加图像。
# Initialize document object | |
document = aspose.pdf.Document() | |
# Add page | |
page = document.pages.add() | |
# Set image coordinates | |
lowerLeftX = 400; | |
lowerLeftY = 400; | |
upperRightX = 150; | |
upperRightY = 150; | |
rectangle= aspose.pdf.Rectangle(lowerLeftX,lowerLeftY,upperRightX,upperRightY,True) | |
# Add image to page | |
page.add_image("aspose-logo.jpg", rectangle) | |
# Save updated PDF | |
document.save("output.pdf") |
您将看到输出的 PDF,如下所示。

使用 Python 在 PDF 中添加表格
插入图像后,让我们在 PDF 文件中添加一个表格。请按照以下步骤编写代码:
- 创建一个 Document 类的对象来创建一个新的 PDF。
- 获取要在其上创建表格的页面。
- 创建 Table 类的一个实例。
- 使用 BorderInfo 类指定表格和单元格的边框。
- 创建新行并将其添加到 Table.Rows 集合。
- 将单元格添加到 Row.Cells 集合。
- 使用 Page.paragraphs.add() 方法将表格添加到页面。
- 使用 Document.Save() 方法保存 PDF 文档。
以下代码示例显示了如何使用 Python 在 PDF 文件中添加表格。
# Initialize document object | |
document = aspose.pdf.Document() | |
# Add page | |
page = document.pages.add() | |
# Create table | |
table = aspose.pdf.Table() | |
# Define table and cell borders | |
table.border=aspose.pdf.BorderInfo(aspose.pdf.BorderSide.ALL,2,aspose.pdf.Color().dark_gray) | |
table.default_cell_border= aspose.pdf.BorderInfo(aspose.pdf.BorderSide.ALL,2,aspose.pdf.Color().black) | |
# Add rows in the table | |
for i in range(1, 11): | |
row = table.rows.add() | |
# Add table cells | |
row.cells.add("Column (" + str(i) + ", 1)"); | |
row.cells.add("Column (" + str(i) + ", 2)"); | |
row.cells.add("Column (" + str(i) + ", 3)"); | |
# Add table to the page | |
page.paragraphs.add(table); | |
# Save updated PDF | |
document.save("D:\\AsposeSampleResults\\output.pdf") |
您将看到输出的 PDF,如下所示。

获得免费许可证
您可以获得 免费临时许可证 来试用 API,而不受评估限制。
加起来
在这篇文章中,我们已经解释了在 Python 中创建 PDF 文件的过程。同样,我们已经描述了如何在 PDF 文档中添加图像、表格和文本格式。
您可以访问 文档 以了解有关 Python PDF 创建程序库 的更多信息。
请继续关注 conholdate.com 以获取定期更新。
问一个问题
您可以在我们的 论坛 上让我们知道您的问题或疑问。