使用IRONPDF FOR PYTHON

Python PdfWriter(代码示例教程)

发布 2023年十二月24日
分享:

简介

IronPDF 是一个纯 Python PDF 文件对象库,适用于希望 编写 PDF 文件 或在其应用程序中操作 PDF 文件。IronPDF 以其简便性和多功能性脱颖而出,是需要自动创建 PDF 或将 PDF 生成集成到软件系统的任务的理想选择。

在本指南中,我们将探讨 IronPDF 这个纯 Python PDF 库如何用于创建 PDF 文件或 PDF 页面属性以及读取 PDF 文件。我们将提供示例和实用代码片段,让您亲身了解如何在 Python 项目中使用 IronPDF for Python 的 PdfWriter 来写入 PDF 文件和创建新的 PDF 页面。

设置 IronPDF

安装

要开始使用 IronPDF,您需要通过 Python 软件包索引安装它。在终端运行以下命令

 pip install ironpdf

编写 PDF 文件和操作 PDF 文件

创建新 PDF

IronPDF 简化了 创建新的 PDF 文件 和处理现有的 PDF 文档。它为生成文档提供了一个直接的界面,无论是简单的一页 PDF 还是包含用户密码等各种元素的更复杂的文档。这一功能对于生成报告、创建发票等任务至关重要。

from ironpdf import *     License.LicenseKey = "Your-License-Key"
# Set a log path
Logger.EnableDebugging = True
Logger.LogFilePath = "Custom.log"
Logger.LoggingMode = Logger.LoggingModes.All
html = """
<html>
   <head>
      <title>IronPDF Python!</title>
      <link rel='stylesheet' href='assets/style.css'>
   </head>
   <body>
      <h1>Its' IronPDF World!!</h1>
      <a href='https://ironpdf.com/python/'><img src='assets/logo.png' /></a>
   </body>
</html>
"""
renderer = ChromePdfRenderer()
pdf = renderer.RenderHtmlAsPdf(html)
pdf.SaveAs("New PDF File.pdf")
PYTHON

Python PdfWriter(代码示例教程):图 1 - 输出文件

合并 PDF 文件

IronPDF 简化了将多个 PDF 文件合并为一个文件的任务。该功能有利于汇总各种报告、组合扫描文档或组织属于一起的信息。例如,当您从多个来源创建一份综合报告时,或者当您有一系列文档需要以单个文件的形式呈现时,您可能需要合并 PDF 文件。

from ironpdf import *     License.LicenseKey = "Your-License-Key"
# Set a log path
Logger.EnableDebugging = True
Logger.LogFilePath = "Custom.log"
Logger.LoggingMode = Logger.LoggingModes.All
pdfOne = PdfDocument("Report First.pdf")
pdfTwo = PdfDocument("Report Second.pdf")
merged = PdfDocument.Merge(pdfOne, pdfTwo)
merged.SaveAs("Merged.pdf")
PYTHON

将现有 PDF 文件合并为一个新的 PDF 文件的功能在数据科学等领域也很有用,因为合并后的 PDF 文件可作为训练人工智能模块的数据集。IronPDF 可以毫不费力地处理这项任务,保持原始文档中每个页面的完整性和格式,从而生成一个无缝、连贯的输出 PDF 文件。

Python PdfWriter(代码示例教程):图 2 - 合并后的 PDF 输出

拆分单个 PDF

相反,IronPDF 还擅长将现有 PDF 文件分割成多个新文件。当您需要从大量 PDF 文档中提取特定部分,或将文档分割成更小、更易于管理的部分时,该功能非常方便。

from ironpdf import *     License.LicenseKey = "Your-License-Key"
# Set a log path
Logger.EnableDebugging = True
Logger.LogFilePath = "Custom.log"
Logger.LoggingMode = Logger.LoggingModes.All
pdf = PdfDocument("Report.pdf")
# take the first page
page1doc = pdf.CopyPage(0)
page1doc.SaveAs("Split1.pdf")
PYTHON

例如,您可能想从一份大型报告中分离出某些 PDF 页面,或从一本书的不同章节中创建单个文档。IronPDF 可让您选择所需的多个页面转换成新的 PDF 文件,确保您可以根据需要操作和管理 PDF 内容。

Python PdfWriter(代码示例教程):图 3 - 分割 PDF 输出

实施安全功能

在处理敏感或机密信息时,确保 PDF 文档的安全是重中之重。IronPDF 通过提供强大的安全功能(包括用户密码保护和加密)来满足这一需求。这确保了 PDF 文件的安全性,只有授权用户才能访问。

from ironpdf import *     License.LicenseKey = "Your-License-Key"
# Set a log path
Logger.EnableDebugging = True
Logger.LogFilePath = "Custom.log"
Logger.LoggingMode = Logger.LoggingModes.All
pdf = PdfDocument("Report.pdf")
# The following code makes a PDF read only and will disallow copy & paste and printing
pdf.SecuritySettings.RemovePasswordsAndEncryption()
pdf.SecuritySettings.MakePdfDocumentReadOnly("secret-key")
pdf.SecuritySettings.AllowUserAnnotations = False
pdf.SecuritySettings.AllowUserCopyPasteContent = False
pdf.SecuritySettings.AllowUserFormData = False
pdf.SecuritySettings.AllowUserPrinting = PdfPrintSecurity.FullPrintRights
# Change or set the document encryption password
pdf.SecuritySettings.OwnerPassword = "top-secret"  # password to edit the PDF
pdf.SecuritySettings.UserPassword = "sharable"  # password to open the PDF
pdf.SaveAs("secured.pdf")
PYTHON

通过设置用户密码,您可以控制谁可以查看或编辑您的 PDF 文档。加密选项增加了额外的安全层,可保护您的数据免受未经授权的访问,使 IronPDF 成为管理 PDF 格式敏感信息的可靠选择。

从 PDF 中提取文本

IronPDF 的另一个重要功能是从 PDF 文档中提取文本。该功能尤其适用于数据检索、内容分析,甚至将现有 PDF 中的文本内容重新组合到新文档中。

from ironpdf import *     License.LicenseKey = "Your-License-Key"
# Set a log path
Logger.EnableDebugging = True
Logger.LogFilePath = "Custom.log"
Logger.LoggingMode = Logger.LoggingModes.All
pdf = PdfDocument("Report.pdf")
# Extract text from PDF document
allText = pdf.ExtractAllText()
# Extract text from specific page in the document
specificPage = pdf.ExtractTextFromPage(3)
PYTHON

无论您是提取数据进行分析、在大型文档中搜索特定信息,还是将 PDF 中的内容转换为文本文件以便进一步处理,IronPDF 都能让您的工作简单而高效。该库可确保提取的文本保持其原始格式和结构,使其能够立即满足您的特定需求。

管理文件信息

对 PDF 文件的高效管理不仅限于其内容。IronPDF 允许有效管理文档元数据和属性,如作者姓名、文档标题、创建日期等。这一功能对于组织和编目 PDF 文档至关重要,尤其是在文档出处和元数据非常重要的环境中。

from ironpdf import *     License.LicenseKey = "Your-License-Key"
# Set a log path
Logger.EnableDebugging = True
Logger.LogFilePath = "Custom.log"
Logger.LoggingMode = Logger.LoggingModes.All
# Load an existing PDF or create a new one
pdf = PdfDocument("Report.pdf")
# Edit file metadata
pdf.MetaData.Author = "Satoshi Nakamoto"
pdf.MetaData.Keywords = "SEO, Friendly"
pdf.MetaData.ModifiedDate = Now()
pdf.SaveAs("MetaData Updated.pdf")
PYTHON

例如,在学术或企业环境中,跟踪文档的创建日期和作者对于记录保存和文档检索至关重要。IronPDF 可轻松管理这些信息,为在 Python 应用程序中处理和更新文档信息提供一种简化的方法。

结论

Python PdfWriter(代码示例教程):图 4 - 许可证

本教程介绍了在 Python 中使用 IronPDF 进行 PDF 操作的基础知识。从创建新的 PDF 文件到合并现有文件,再到添加安全功能,IronPDF 对于任何 Python 开发人员来说都是一个多功能工具。

IronPDF for Python 提供了一个 免费试用 供用户探索其功能。对于试用期后的继续使用,许可证起价为"$liteLicense"。这种定价方式允许开发人员在其项目中使用 IronPDF 的全部功能。

< 前一页
如何在Python中为PDF文件添加水印
下一步 >
如何在Python中从扫描的PDF中提取文本

准备开始了吗? 版本: 2024.9 刚刚发布

免费 pip 安装 查看许可证 >