{"id":961394,"date":"2024-12-27T03:58:21","date_gmt":"2024-12-26T19:58:21","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/961394.html"},"modified":"2024-12-27T03:58:23","modified_gmt":"2024-12-26T19:58:23","slug":"python%e5%a6%82%e4%bd%95%e5%90%88%e5%b9%b6%e6%88%90pdf","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/961394.html","title":{"rendered":"python\u5982\u4f55\u5408\u5e76\u6210pdf"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25103634\/35911751-b12f-447c-9511-3e80e13feb15.webp\" alt=\"python\u5982\u4f55\u5408\u5e76\u6210pdf\" \/><\/p>\n<p><p> \u5728Python\u4e2d\u5408\u5e76\u591a\u4e2a\u6587\u4ef6\uff08\u5982\u6587\u672c\u6216\u56fe\u50cf\uff09\u6210\u4e00\u4e2aPDF\u6587\u4ef6\u7684\u8fc7\u7a0b\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u591a\u4e2a\u5e93\u6765\u5b9e\u73b0\u3002<strong>Python\u4e2d\u5408\u5e76\u6587\u4ef6\u6210PDF\u7684\u5e38\u7528\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528PyPDF2\u3001Fpdf\u548cReportLab\u5e93<\/strong>\u3002\u5176\u4e2d\uff0cPyPDF2\u7528\u4e8e\u5408\u5e76\u73b0\u6709PDF\u6587\u4ef6\uff0cFpdf\u7528\u4e8e\u751f\u6210\u65b0\u7684PDF\u6587\u4ef6\uff0c\u800cReportLab\u5219\u63d0\u4f9b\u4e86\u66f4\u5f3a\u5927\u7684\u529f\u80fd\u7528\u4e8e\u521b\u5efa\u590d\u6742\u7684PDF\u6587\u6863\u3002\u4ee5\u4e0b\u662f\u5bf9\u5176\u4e2d\u4e00\u4e2a\u65b9\u6cd5\u7684\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528PyPDF2\u5e93<\/strong>\uff1a\u8fd9\u4e2a\u5e93\u4e3b\u8981\u7528\u4e8e\u5408\u5e76\u591a\u4e2a\u73b0\u6709\u7684PDF\u6587\u4ef6\u3002\u9996\u5148\uff0c\u4f60\u9700\u8981\u5b89\u88c5PyPDF2\u5e93\uff0c\u53ef\u4ee5\u901a\u8fc7pip\u547d\u4ee4\u6765\u5b9e\u73b0\u3002\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u4f7f\u7528PyPDF2\u5e93\u4e2d\u7684PdfMerger\u7c7b\u6765\u5408\u5e76PDF\u6587\u4ef6\u3002\u5177\u4f53\u6b65\u9aa4\u5305\u62ec\u521b\u5efaPdfMerger\u5bf9\u8c61\u3001\u5c06PDF\u6587\u4ef6\u6dfb\u52a0\u5230\u5bf9\u8c61\u4e2d\uff0c\u5e76\u8c03\u7528write\u65b9\u6cd5\u5c06\u5408\u5e76\u7684\u5185\u5bb9\u5199\u5165\u65b0\u7684PDF\u6587\u4ef6\u3002PyPDF2\u5e93\u64cd\u4f5c\u7b80\u5355\uff0c\u9002\u5408\u9700\u8981\u5408\u5e76\u73b0\u6709PDF\u6587\u4ef6\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><p>\u4e0b\u9762\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u4e0d\u540c\u7684Python\u5e93\u6765\u5408\u5e76\u6587\u4ef6\u6210PDF\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u4f7f\u7528PyPDF2\u5e93\u5408\u5e76PDF\u6587\u4ef6<\/h2>\n<\/p>\n<p><p>PyPDF2\u662f\u4e00\u4e2a\u7eafPython\u7f16\u5199\u7684\u5e93\uff0c\u63d0\u4f9b\u4e86\u5bf9PDF\u6587\u4ef6\u7684\u57fa\u672c\u5904\u7406\u529f\u80fd\uff0c\u5982\u5408\u5e76\u3001\u62c6\u5206\u3001\u65cb\u8f6c\u7b49\u3002<\/p>\n<\/p>\n<p><h3>1\u3001\u5b89\u88c5PyPDF2\u5e93<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u4f7f\u7528pip\u547d\u4ee4\u5b89\u88c5PyPDF2\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install PyPDF2<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2\u3001\u5408\u5e76PDF\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528PdfMerger\u7c7b\u53ef\u4ee5\u8f7b\u677e\u5408\u5e76\u591a\u4e2aPDF\u6587\u4ef6\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PyPDF2 import PdfMerger<\/p>\n<p>def merge_pdfs(pdf_list, output_path):<\/p>\n<p>    merger = PdfMerger()<\/p>\n<p>    for pdf in pdf_list:<\/p>\n<p>        merger.append(pdf)<\/p>\n<p>    merger.write(output_path)<\/p>\n<p>    merger.close()<\/p>\n<h2><strong>\u793a\u4f8b\u4f7f\u7528<\/strong><\/h2>\n<p>pdf_files = [&#39;file1.pdf&#39;, &#39;file2.pdf&#39;, &#39;file3.pdf&#39;]<\/p>\n<p>merge_pdfs(pdf_files, &#39;merged.pdf&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a<code>merge_pdfs<\/code>\u51fd\u6570\uff0c\u5b83\u63a5\u53d7\u4e00\u4e2a\u5305\u542bPDF\u6587\u4ef6\u8def\u5f84\u7684\u5217\u8868\u548c\u8f93\u51fa\u6587\u4ef6\u7684\u8def\u5f84\u3002<code>PdfMerger<\/code>\u5bf9\u8c61\u7528\u4e8e\u5408\u5e76PDF\u6587\u4ef6\uff0c<code>append<\/code>\u65b9\u6cd5\u5c06\u6bcf\u4e2aPDF\u6587\u4ef6\u6dfb\u52a0\u5230\u5408\u5e76\u5217\u8868\u4e2d\uff0c\u6700\u540e\u8c03\u7528<code>write<\/code>\u65b9\u6cd5\u8f93\u51fa\u5408\u5e76\u540e\u7684PDF\u3002<\/p>\n<\/p>\n<p><h2>\u4e8c\u3001\u4f7f\u7528Fpdf\u5e93\u751f\u6210PDF<\/h2>\n<\/p>\n<p><p>Fpdf\u5e93\u9002\u7528\u4e8e\u751f\u6210\u65b0\u7684PDF\u6587\u4ef6\uff0c\u8fd9\u5728\u9700\u8981\u4ece\u6587\u672c\u3001\u56fe\u50cf\u7b49\u751f\u6210PDF\u65f6\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><h3>1\u3001\u5b89\u88c5Fpdf\u5e93<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5Fpdf\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install fpdf<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2\u3001\u751f\u6210PDF\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4f7f\u7528Fpdf\u5e93\u751f\u6210PDF\u6587\u4ef6\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from fpdf import FPDF<\/p>\n<p>class PDF(FPDF):<\/p>\n<p>    def header(self):<\/p>\n<p>        self.set_font(&#39;Arial&#39;, &#39;B&#39;, 12)<\/p>\n<p>        self.cell(0, 10, &#39;Document Title&#39;, align=&#39;C&#39;, ln=True)<\/p>\n<p>    def footer(self):<\/p>\n<p>        self.set_y(-15)<\/p>\n<p>        self.set_font(&#39;Arial&#39;, &#39;I&#39;, 8)<\/p>\n<p>        self.cell(0, 10, f&#39;Page {self.page_no()}&#39;, align=&#39;C&#39;)<\/p>\n<p>def create_pdf(output_path):<\/p>\n<p>    pdf = PDF()<\/p>\n<p>    pdf.add_page()<\/p>\n<p>    pdf.set_font(&#39;Arial&#39;, &#39;B&#39;, 16)<\/p>\n<p>    pdf.cell(40, 10, &#39;Hello World!&#39;)<\/p>\n<p>    pdf.output(output_path)<\/p>\n<h2><strong>\u793a\u4f8b\u4f7f\u7528<\/strong><\/h2>\n<p>create_pdf(&#39;generated.pdf&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a<code>PDF<\/code>\u7c7b\u7ee7\u627f\u81ea<code>FPDF<\/code>\uff0c\u5e76\u91cd\u5199\u4e86<code>header<\/code>\u548c<code>footer<\/code>\u65b9\u6cd5\u4ee5\u81ea\u5b9a\u4e49\u9875\u7709\u548c\u9875\u811a\u3002<code>create_pdf<\/code>\u51fd\u6570\u7528\u4e8e\u751f\u6210PDF\u6587\u4ef6\u5e76\u4fdd\u5b58\u5230\u6307\u5b9a\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><h2>\u4e09\u3001\u4f7f\u7528ReportLab\u5e93\u521b\u5efa\u590d\u6742PDF<\/h2>\n<\/p>\n<p><p>ReportLab\u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u7684\u5e93\uff0c\u9002\u5408\u9700\u8981\u751f\u6210\u590d\u6742\u5e03\u5c40\u7684PDF\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h3>1\u3001\u5b89\u88c5ReportLab\u5e93<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5ReportLab\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install reportlab<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2\u3001\u521b\u5efaPDF\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4f7f\u7528ReportLab\u5e93\u521b\u5efaPDF\u6587\u4ef6\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from reportlab.lib.pagesizes import letter<\/p>\n<p>from reportlab.pdfgen import canvas<\/p>\n<p>def create_complex_pdf(output_path):<\/p>\n<p>    c = canvas.Canvas(output_path, pagesize=letter)<\/p>\n<p>    width, height = letter<\/p>\n<p>    c.setFont(&quot;Helvetica-Bold&quot;, 20)<\/p>\n<p>    c.drawCentredString(width \/ 2.0, height - 100, &quot;Complex PDF Document&quot;)<\/p>\n<p>    c.setFont(&quot;Helvetica&quot;, 12)<\/p>\n<p>    c.drawString(100, height - 150, &quot;This is an example of a complex PDF document created using ReportLab.&quot;)<\/p>\n<p>    c.showPage()<\/p>\n<p>    c.save()<\/p>\n<h2><strong>\u793a\u4f8b\u4f7f\u7528<\/strong><\/h2>\n<p>create_complex_pdf(&#39;complex.pdf&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>canvas.Canvas<\/code>\u5bf9\u8c61\u521b\u5efa\u4e00\u4e2a\u65b0\u7684PDF\u6587\u4ef6\u3002<code>drawCentredString<\/code>\u548c<code>drawString<\/code>\u65b9\u6cd5\u7528\u4e8e\u5728PDF\u4e2d\u7ed8\u5236\u6587\u672c\u3002<code>showPage<\/code>\u65b9\u6cd5\u7528\u4e8e\u6dfb\u52a0\u65b0\u9875\u9762\uff0c<code>save<\/code>\u65b9\u6cd5\u7528\u4e8e\u4fdd\u5b58PDF\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h2>\u56db\u3001\u603b\u7ed3\u4e0e\u6700\u4f73\u5b9e\u8df5<\/h2>\n<\/p>\n<p><p>\u5728Python\u4e2d\u5408\u5e76\u6216\u751f\u6210PDF\u6587\u4ef6\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u9009\u62e9\u9002\u5408\u7684\u5e93\u53d6\u51b3\u4e8e\u5177\u4f53\u9700\u6c42\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>PyPDF2<\/strong>\uff1a\u9002\u7528\u4e8e\u5408\u5e76\u6216\u64cd\u4f5c\u73b0\u6709PDF\u6587\u4ef6\uff0c\u7b80\u5355\u6613\u7528\u3002<\/li>\n<li><strong>Fpdf<\/strong>\uff1a\u9002\u7528\u4e8e\u751f\u6210\u65b0\u7684PDF\u6587\u4ef6\uff0c\u652f\u6301\u6587\u672c\u548c\u56fe\u50cf\u3002<\/li>\n<li><strong>ReportLab<\/strong>\uff1a\u9002\u7528\u4e8e\u521b\u5efa\u590d\u6742\u5e03\u5c40\u7684PDF\uff0c\u529f\u80fd\u5f3a\u5927\u4f46\u4f7f\u7528\u7a0d\u590d\u6742\u3002<\/li>\n<\/ul>\n<p><p>\u5728\u4f7f\u7528\u8fd9\u4e9b\u5e93\u65f6\uff0c\u9700\u8981\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u5de5\u5177\uff0c\u5e76\u6ce8\u610f\u5904\u7406\u6587\u4ef6\u8def\u5f84\u548c\u53ef\u80fd\u7684\u5f02\u5e38\u60c5\u51b5\u3002\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\uff0cPython\u5f00\u53d1\u8005\u53ef\u4ee5\u7075\u6d3b\u5730\u751f\u6210\u548c\u64cd\u4f5cPDF\u6587\u4ef6\uff0c\u6ee1\u8db3\u5404\u79cd\u5e94\u7528\u573a\u666f\u7684\u9700\u6c42\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u5408\u5e76\u591a\u4e2aPDF\u6587\u4ef6\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528PyPDF2\u6216pdfrw\u7b49\u5e93\u6765\u5408\u5e76PDF\u6587\u4ef6\u3002\u9996\u5148\uff0c\u786e\u4fdd\u5b89\u88c5\u4e86\u6240\u9700\u7684\u5e93\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528PyPDF2\u7684\u57fa\u672c\u6b65\u9aa4\uff1a<\/p>\n<ol>\n<li>\u5bfc\u5165\u6240\u9700\u7684\u5e93\u3002<\/li>\n<li>\u521b\u5efa\u4e00\u4e2aPDF\u6587\u4ef6\u5408\u5e76\u5668\u5bf9\u8c61\u3002<\/li>\n<li>\u9010\u4e2a\u6dfb\u52a0\u8981\u5408\u5e76\u7684PDF\u6587\u4ef6\u3002<\/li>\n<li>\u6700\u540e\uff0c\u8c03\u7528\u5199\u5165\u65b9\u6cd5\u751f\u6210\u5408\u5e76\u540e\u7684PDF\u6587\u4ef6\u3002<\/li>\n<\/ol>\n<p><strong>Python\u5408\u5e76PDF\u65f6\u53ef\u4ee5\u5904\u7406\u54ea\u4e9b\u7c7b\u578b\u7684\u6587\u4ef6\uff1f<\/strong><br \/>\u901a\u5e38\uff0c\u5408\u5e76\u7684\u6587\u4ef6\u5e94\u4e3a\u6807\u51c6\u7684PDF\u683c\u5f0f\u3002\u5982\u679c\u60a8\u5c1d\u8bd5\u5408\u5e76\u7684\u6587\u4ef6\u662f\u5176\u4ed6\u683c\u5f0f\uff08\u5982Word\u6587\u6863\u6216\u56fe\u50cf\u6587\u4ef6\uff09\uff0c\u5219\u9700\u8981\u5148\u5c06\u5b83\u4eec\u8f6c\u6362\u4e3aPDF\u683c\u5f0f\u3002\u4f7f\u7528Python\u7684\u5176\u4ed6\u5e93\uff0c\u5982pdf2image\u6216reportlab\uff0c\u53ef\u4ee5\u5e2e\u52a9\u60a8\u5c06\u8fd9\u4e9b\u6587\u4ef6\u8f6c\u6362\u4e3aPDF\u3002<\/p>\n<p><strong>\u5408\u5e76PDF\u6587\u4ef6\u65f6\uff0c\u5982\u4f55\u4fdd\u6301\u9875\u9762\u987a\u5e8f\u548c\u8d28\u91cf\uff1f<\/strong><br \/>\u5728\u5408\u5e76PDF\u6587\u4ef6\u65f6\uff0c\u786e\u4fdd\u6309\u7167\u6240\u9700\u7684\u987a\u5e8f\u5c06\u6587\u4ef6\u6dfb\u52a0\u5230\u5408\u5e76\u5668\u4e2d\u3002\u5927\u591a\u6570\u5e93\u4f1a\u4fdd\u7559\u539f\u6587\u4ef6\u7684\u8d28\u91cf\uff0c\u56e0\u6b64\u4e0d\u9700\u8981\u62c5\u5fc3\u5408\u5e76\u540e\u6587\u4ef6\u7684\u6e05\u6670\u5ea6\u3002\u60a8\u53ef\u4ee5\u901a\u8fc7\u67e5\u770b\u5408\u5e76\u540e\u7684\u6587\u4ef6\uff0c\u786e\u8ba4\u9875\u9762\u987a\u5e8f\u548c\u8d28\u91cf\u662f\u5426\u7b26\u5408\u9884\u671f\u3002\u5982\u679c\u8d28\u91cf\u4e0d\u7b26\u5408\u8981\u6c42\uff0c\u53ef\u4ee5\u8003\u8651\u8c03\u6574PDF\u751f\u6210\u8bbe\u7f6e\u6216\u9009\u62e9\u5176\u4ed6\u5408\u5e76\u5de5\u5177\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u5408\u5e76\u591a\u4e2a\u6587\u4ef6\uff08\u5982\u6587\u672c\u6216\u56fe\u50cf\uff09\u6210\u4e00\u4e2aPDF\u6587\u4ef6\u7684\u8fc7\u7a0b\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u591a\u4e2a\u5e93\u6765\u5b9e\u73b0\u3002Python\u4e2d\u5408 [&hellip;]","protected":false},"author":3,"featured_media":961400,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/961394"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=961394"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/961394\/revisions"}],"predecessor-version":[{"id":961404,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/961394\/revisions\/961404"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/961400"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=961394"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=961394"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=961394"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}