使用 IRONPDF FOR PYTHON 如何在 Python 中编辑 PDF 文件 Curtis Chau 已更新:六月 22, 2025 Download IronPDF pip 下载 Start Free Trial Copy for LLMs Copy for LLMs Copy page as Markdown for LLMs Open in ChatGPT Ask ChatGPT about this page Open in Gemini Ask Gemini about this page Open in Grok Ask Grok about this page Open in Perplexity Ask Perplexity about this page Share Share on Facebook Share on X (Twitter) Share on LinkedIn Copy URL Email article Iron Software 推出了 IronPDF for Python 库,该解决方案彻底改变了在 Python 中执行 PDF 编辑任务的便捷性。 无论您是需要插入签名、添加 HTML 页脚、嵌入水印、包含注释还是编辑 PDF 文件,IronPDF for Python 都是您的得力助手。 该库可确保代码的可读性,支持以编程方式创建 PDF,便于直接调试,并可在所有兼容平台和环境中无缝部署。 本教程文章将通过 Python 代码示例和全面的解释来探讨这些广泛的功能。 在本指南结束时,您将对如何使用 IronPDF for Python 满足所有 PDF 编辑需求有一个坚实的了解。 如何用 Python 编辑 PDF 文件编辑文档结构操作页面添加页面Cover Page") # Prepend new page to existing PDF pdf.PrependPdf(coverPagePdf) # Save the updated PDF document pdf.SaveAs("report_with_cover.pdf") ``` 复制页面删除页面合并和拆分 PDF 文件将多个现有 PDF 合并为一个 PDF 文档 [PDF_A] [PDF_A] 1st Page [PDF_A] 2nd Page""" html_b = """ [PDF_B] [PDF_B] 1st Page [PDF_B] 2nd Page""" # Render each HTML content as PDF renderer = ChromePdfRenderer() pdfdoc_a = renderer.RenderHtmlAsPdf(html_a) pdfdoc_b = renderer.RenderHtmlAsPdf(html_b) # Merge the PDFs into a single document merged = PdfDocument.Merge(pdfdoc_a, pdfdoc_b) # Save the merged PDF merged.SaveAs("Merged.pdf") ``` 分割 PDF 和提取页面 Hello Iron This is 1st Page This is 2nd Page This is 3rd Page""" # Render the HTML as a PDF document renderer = ChromePdfRenderer() pdf = renderer.RenderHtmlAsPdf(html) # Create a separate document for the first page page1doc = pdf.CopyPage(0) page1doc.SaveAs("Split1.pdf") # Create a separate document for pages 2 and 3 page23doc = pdf.CopyPages(1, 2) page23doc.SaveAs("Split2.pdf") ``` 编辑文档属性添加和使用 PDF 元数据数字签名Testing 2048 bit digital security") # Step 2. Create a digital signature. signature = PdfSignature(r"certificates\IronSoftware.pfx", "123456") # Step 3. Optional signing options and a handwritten signature graphic. signature.SigningContact = "support@ironsoftware.com" signature.SigningLocation = "Chicago, USA" signature.SigningReason = "To show how to sign a PDF" # Step 4. Sign the PDF with the PdfSignature. doc.Sign(signature) # Step 5. The PDF is not signed until saved to file, stream, or byte array. doc.SaveAs("signed.pdf") ``` PDF附件压缩 PDF 文件编辑 PDF 内容添加页眉和页脚HTML 页眉和页脚{page} of {total-pages}" renderer.RenderingOptions.HtmlFooter.DrawDividerLine = True # Ensure sufficient bottom margin renderer.RenderingOptions.MarginBottom = 25 # mm # Build a header using an image asset renderer.RenderingOptions.HtmlHeader = HtmlHeaderFooter() renderer.RenderingOptions.HtmlHeader.MaxHeight = 20 # millimeters renderer.RenderingOptions.HtmlHeader.HtmlFragment = "" renderer.RenderingOptions.HtmlHeader.BaseUrl = os.path.abspath("C:/Users/lyty1/OneDrive/Documents/IronPdfPythonNew") # Ensure sufficient top margin renderer.RenderingOptions.MarginTop = 25 # mm ``` 页眉和页脚文本大纲和书签添加和编辑注释添加背景和前景标记和水印在 PDF 上应用印章") stamper.HorizontalAlignment = HorizontalAlignment.Center stamper.VerticalAlignment = VerticalAlignment.Bottom stamper.IsStampBehindContent = False stamper.Opacity = 30 # Load existing PDF and apply the stamp pdf = PdfDocument.FromFile("Sample.pdf") pdf.ApplyStamp(stamper).SaveAs("stampedimage.pdf") ``` 为 PDF 添加水印SAMPLE", 30, VerticalAlignment.Middle, HorizontalAlignment.Center) # Save the watermarked PDF pdf.SaveAs("Watermarked.pdf") ``` 在 PDF 中使用表单创建和编辑表单 Editable PDF Form First name: Last name: Please specify your gender: Female Male Non-Binary / Other Please select all medical conditions that apply: Hypertension Heart Disease Stroke Diabetes Kidney Disease