{"id":220323,"date":"2024-05-11T18:15:50","date_gmt":"2024-05-11T10:15:50","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/220323.html"},"modified":"2024-05-11T18:15:57","modified_gmt":"2024-05-11T10:15:57","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8python%e5%88%b6%e4%bd%9cpdf%e6%96%87%e6%a1%a3-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/220323.html","title":{"rendered":"\u5982\u4f55\u4f7f\u7528Python\u5236\u4f5cpdf\u6587\u6863"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25140559\/3554792b-5e33-44c5-87c2-99114b17e4c3.webp\" alt=\"\u5982\u4f55\u4f7f\u7528Python\u5236\u4f5cpdf\u6587\u6863\" \/><\/p>\n<p><p>\u5728\u8fd9\u4e2a\u6570\u5b57\u5316\u65f6\u4ee3\uff0c\u81ea\u52a8\u5316\u5730\u751f\u6210PDF\u6587\u6863\u53d8\u5f97\u8d8a\u6765\u8d8a\u91cd\u8981\u3002<strong>\u4f7f\u7528Python\u5236\u4f5cPDF\u6587\u6863\u7684\u4e3b\u8981\u65b9\u5f0f\u5305\u62ec\uff1a\u4f7f\u7528ReportLab\u5e93\u3001PDFKit\u5e93\u3001PyPDF2\u5e93\u3001\u4ee5\u53caFPDF\u5e93<\/strong>\u3002\u8fd9\u4e9b\u65b9\u6cd5\u5404\u6709\u7279\u70b9\uff0c\u4f46<strong>ReportLab\u5e93<\/strong>\u5c24\u5176\u53d7\u5230\u91cd\u89c6\uff0c\u56e0\u5176\u529f\u80fd\u5f3a\u5927\u3001\u7075\u6d3b\u6027\u9ad8\u3001\u800c\u4e14\u652f\u6301\u590d\u6742\u7684\u56fe\u5f62\u548c\u56fe\u8868\u3002\u5b83\u5141\u8bb8\u5f00\u53d1\u4eba\u5458\u4ece\u5934\u5f00\u59cb\u521b\u5efaPDF\u6587\u4ef6\uff0c\u4e5f\u652f\u6301\u63d2\u5165\u56fe\u50cf\u3001\u8868\u683c\u3001\u81ea\u5b9a\u4e49\u5b57\u4f53\u7b49\u9ad8\u7ea7\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528REPORTLAB\u5e93\u521b\u5efaPDF<\/h3>\n<\/p>\n<p><p>ReportLab\u5e93\u662fPython\u4e2d\u6700\u5f3a\u5927\u7684PDF\u751f\u6210\u5e93\u4e4b\u4e00\u3002\u8be5\u5e93\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684API\u7528\u4e8e\u521b\u5efa\u6587\u672c\u3001\u56fe\u5f62\u548c\u56fe\u8868\u3002<\/p>\n<\/p>\n<ul>\n<li>\u9996\u5148\uff0c\u5b89\u88c5ReportLab\u5e93\u3002\u5728\u547d\u4ee4\u884c\u4e2d\u8f93\u5165<code>pip install reportlab<\/code>\u5373\u53ef\u5b89\u88c5\u3002<\/li>\n<li>\u7136\u540e\uff0c\u5bfc\u5165ReportLab\u76f8\u5173\u6a21\u5757\u3002\u4ecereportlab.pdfgen\u5bfc\u5165canvas\u6a21\u5757\uff0c\u5b83\u662f\u751f\u6210PDF\u6587\u4ef6\u7684\u57fa\u7840\u3002<\/li>\n<\/ul>\n<p><pre><code class=\"language-python\">from reportlab.pdfgen import canvas<\/p>\n<p>def create_pdf(file_path):<\/p>\n<p>    c = canvas.Canvas(file_path)<\/p>\n<p>    c.drawString(100, 750, &quot;Welcome to ReportLab!&quot;)<\/p>\n<p>    c.save()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4ee5\u4e0a\u4ee3\u7801\u6f14\u793a\u4e86\u5982\u4f55\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684PDF\u6587\u4ef6\uff0c\u5176\u4e2d\u5305\u542b\u201cWelcome to ReportLab!\u201d\u6587\u5b57\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528PDFKIT\u5e93\u751f\u6210PDF<\/h3>\n<\/p>\n<p><p>PDFKit\u662f\u53e6\u4e00\u4e2a\u53ef\u4ee5\u7528\u6765\u751f\u6210PDF\u7684Python\u5e93\uff0c\u5b83\u662fwkhtmltopdf\u7684\u5c01\u88c5\u3002<\/p>\n<\/p>\n<ul>\n<li>\u5b89\u88c5PDFKit\u4e4b\u524d\uff0c\u9700\u8981\u5148\u5b89\u88c5wkhtmltopdf\u3002\u6839\u636e\u64cd\u4f5c\u7cfb\u7edf\u4e0d\u540c\uff0c\u5b89\u88c5\u65b9\u6cd5\u4f1a\u6709\u6240\u4e0d\u540c\u3002<\/li>\n<li>\u4f7f\u7528<code>pip install pdfkit<\/code>\u547d\u4ee4\u6765\u5b89\u88c5PDFKit\u5e93\u3002<\/li>\n<\/ul>\n<p><pre><code class=\"language-python\">import pdfkit<\/p>\n<p>def create_pdf_with_pdfkit(url, output_file):<\/p>\n<p>    pdfkit.from_url(url, output_file)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6b64\u4ee3\u7801\u6f14\u793a\u4e86\u5982\u4f55\u4ece\u7f51\u9875URL\u751f\u6210PDF\u6587\u4ef6\u3002PDFKit\u4e5f\u652f\u6301\u4eceHTML\u5b57\u7b26\u4e32\u6216\u6587\u4ef6\u751f\u6210PDF\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u5229\u7528PyPDF2\u5e93\u5904\u7406PDF<\/h3>\n<\/p>\n<p><p>PyPDF2\u662f\u4e00\u4e2a\u7eafPython\u5e93\uff0c\u7528\u4e8e\u8bfb\u53d6\u548c\u5199\u5165PDF\u6587\u4ef6\uff0c\u4e0d\u652f\u6301\u521b\u5efa\u65b0\u7684PDF\u6587\u4ef6\uff0c\u4f46\u5b83\u975e\u5e38\u9002\u5408\u4e8ePDF\u6587\u6863\u7684\u5408\u5e76\u3001\u62c6\u5206\u3001\u52a0\u5bc6\u7b49\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<ul>\n<li>\u5b89\u88c5PyPDF2\u5e93\uff0c\u4f7f\u7528\u547d\u4ee4<code>pip install PyPDF2<\/code>\u3002<\/li>\n<li>\u4f7f\u7528PyPDF2\uff0c\u53ef\u4ee5\u7b80\u5355\u5730\u5408\u5e76\u591a\u4e2aPDF\u6587\u6863\u6216\u63d0\u53d6\u9875\u9762\u3002<\/li>\n<\/ul>\n<p><pre><code class=\"language-python\">from PyPDF2 import PdfReader, PdfWriter<\/p>\n<p>def merge_pdfs(paths, output):<\/p>\n<p>    pdf_writer = PdfWriter()<\/p>\n<p>    for path in paths:<\/p>\n<p>        pdf_reader = PdfReader(path)<\/p>\n<p>        for page_num in range(len(pdf_reader.pages)):<\/p>\n<p>            page = pdf_reader.pages[page_num]<\/p>\n<p>            pdf_writer.add_page(page)<\/p>\n<p>    with open(output, &#039;wb&#039;) as out:<\/p>\n<p>        pdf_writer.write(out)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528PyPDF2\u5408\u5e76\u591a\u4e2aPDF\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u901a\u8fc7FPDF\u5e93\u751f\u6210PDF<\/h3>\n<\/p>\n<p><p>FPDF\u662f\u4e00\u4e2a\u7528\u4e8e\u751f\u6210PDF\u6587\u6863\u7684Python\u5e93\uff0c\u5b83\u5141\u8bb8\u5f00\u53d1\u4eba\u5458\u4ee5\u66f4\u76f4\u63a5\u7684\u65b9\u5f0f\u64cd\u4f5cPDF\uff0c\u5982\u6dfb\u52a0\u6587\u672c\u3001\u7ebf\u6761\u3001\u56fe\u50cf\u7b49\u3002<\/p>\n<\/p>\n<ul>\n<li>FPDF\u5e93\u901a\u8fc7<code>pip install fpdf<\/code>\u547d\u4ee4\u5b89\u88c5\u3002<\/li>\n<li>\u4f7f\u7528FPDF\u521b\u5efaPDF\u6587\u6863\u5341\u5206\u76f4\u63a5\uff0c\u53ea\u9700\u8981\u521b\u5efaFPDF\u5bf9\u8c61\uff0c\u7136\u540e\u589e\u52a0\u9875\u9762\u548c\u6587\u672c\u5373\u53ef\u3002<\/li>\n<\/ul>\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>        # \u53ef\u4ee5\u5728\u8fd9\u91cc\u6dfb\u52a0\u6807\u9898\u5934<\/p>\n<p>        self.set_font(&#039;Arial&#039;, &#039;B&#039;, 12)<\/p>\n<p>        self.cell(0, 10, &#039;Title&#039;, 0, 1, &#039;C&#039;)<\/p>\n<p>pdf = PDF()<\/p>\n<p>pdf.add_page()<\/p>\n<p>pdf.set_font(&quot;Arial&quot;, size=12)<\/p>\n<p>pdf.cell(0, 10, &quot;Hello World&quot;, 0, 1)<\/p>\n<p>pdf.output(&quot;simple_demo.pdf&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4ee5\u4e0a\u4ee3\u7801\u6f14\u793a\u4e86FPDF\u5e93\u7684\u57fa\u672c\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><p>\u7efc\u4e0a\u6240\u8ff0\uff0c\u6839\u636e\u4e0d\u540c\u7684\u9700\u6c42\u548c\u573a\u666f\uff0c\u53ef\u4ee5\u9009\u62e9\u5408\u9002\u7684\u5e93\u6765\u5236\u4f5cPDF\u6587\u6863\u3002\u65e0\u8bba\u662f\u521b\u5efa\u4e30\u5bcc\u591a\u5f69\u7684\u62a5\u544a\u3001\u81ea\u52a8\u5316\u751f\u6210\u53d1\u7968\u8fd8\u662f\u7b80\u5355\u6587\u6863\u7684\u5904\u7406\uff0cPython\u90fd\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u5e93\u652f\u6301\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. \u6709\u6ca1\u6709\u9002\u5408\u521d\u5b66\u8005\u7684Python\u56fe\u4e66\u63a8\u8350\uff1f<\/strong> <\/p>\n<p>\u5f53\u7136\u6709\uff01\u5bf9\u4e8e\u521d\u5b66\u8005\uff0c\u63a8\u8350\u300aPython\u7f16\u7a0b\u5feb\u901f\u4e0a\u624b\u300b\u3001\u300aPython\u7f16\u7a0b\u4ece\u5165\u95e8\u5230\u5b9e\u8df5\u300b\u7b49\u5165\u95e8\u7ea7\u56fe\u4e66\u3002\u5b83\u4eec\u901a\u5e38\u4f1a\u4ecb\u7ecdPython\u7684\u57fa\u672c\u8bed\u6cd5\u3001\u5e38\u7528\u6a21\u5757\u548c\u51fd\u6570\uff0c\u5e76\u901a\u8fc7\u5b9e\u9645\u6848\u4f8b\u5e2e\u52a9\u4f60\u5feb\u901f\u5165\u95e8\u3002<\/p>\n<p><strong>2. Python\u4e2d\u6709\u54ea\u4e9b\u5e38\u7528\u7684\u5904\u7406PDF\u6587\u6863\u7684\u5e93\uff1f<\/strong> <\/p>\n<p>Python\u6709\u4e00\u4e9b\u5f3a\u5927\u7684\u7b2c\u4e09\u65b9\u5e93\u53ef\u4ee5\u5e2e\u52a9\u4f60\u5904\u7406PDF\u6587\u6863\u3002\u4f8b\u5982\uff0cPyPDF2\u53ef\u4ee5\u7528\u4e8e\u8bfb\u53d6\u548c\u63d0\u53d6PDF\u6587\u4ef6\u4e2d\u7684\u6587\u672c\u3001\u56fe\u50cf\u6216\u5143\u6570\u636e\u4fe1\u606f\uff1bReportLab\u53ef\u4ee5\u7528\u4e8e\u751f\u6210PDF\u6587\u6863\u5e76\u6dfb\u52a0\u6587\u672c\u3001\u56fe\u50cf\u3001\u8868\u683c\u7b49\uff1bpdfminer\u53ef\u4ee5\u7528\u4e8e\u89e3\u6790PDF\u6587\u6863\u5e76\u63d0\u53d6\u6587\u672c\u5185\u5bb9\u7b49\u3002<\/p>\n<p><strong>3. \u5982\u4f55\u4f7f\u7528Python\u751f\u6210\u5305\u542b\u591a\u4e2a\u9875\u9762\u7684PDF\u6587\u6863\uff1f<\/strong> <\/p>\n<p>\u8981\u521b\u5efa\u5305\u542b\u591a\u4e2a\u9875\u9762\u7684PDF\u6587\u6863\uff0c\u53ef\u4ee5\u4f7f\u7528ReportLab\u5e93\u3002\u9996\u5148\uff0c\u5bfc\u5165\u5fc5\u8981\u7684\u6a21\u5757\uff1b\u7136\u540e\uff0c\u521b\u5efa\u4e00\u4e2aCanvas\u5bf9\u8c61\uff0c\u6307\u5b9a\u6587\u6863\u5c3a\u5bf8\u548c\u80cc\u666f\u8bbe\u7f6e\uff1b\u63a5\u7740\uff0c\u4f7f\u7528Canvas\u5bf9\u8c61\u7684\u65b9\u6cd5\u6dfb\u52a0\u5185\u5bb9\uff0c\u5982\u6587\u672c\u3001\u56fe\u50cf\u3001\u8868\u683c\u7b49\uff1b\u6700\u540e\uff0c\u4fdd\u5b58\u5e76\u5173\u95ed\u6587\u6863\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u5faa\u73af\u6765\u6dfb\u52a0\u591a\u4e2a\u9875\u9762\uff0c\u5e76\u5728\u6bcf\u4e2a\u9875\u9762\u4e0a\u6dfb\u52a0\u4e0d\u540c\u7684\u5185\u5bb9\u3002\u8bb0\u5f97\u5728\u751f\u6210PDF\u6587\u6863\u4e4b\u524d\uff0c\u5148\u5b89\u88c5ReportLab\u5e93\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728\u8fd9\u4e2a\u6570\u5b57\u5316\u65f6\u4ee3\uff0c\u81ea\u52a8\u5316\u5730\u751f\u6210PDF\u6587\u6863\u53d8\u5f97\u8d8a\u6765\u8d8a\u91cd\u8981\u3002\u4f7f\u7528Python\u5236\u4f5cPDF\u6587\u6863\u7684\u4e3b\u8981\u65b9\u5f0f\u5305\u62ec\uff1a\u4f7f\u7528Re [&hellip;]","protected":false},"author":3,"featured_media":220331,"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\/220323"}],"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=220323"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/220323\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/220331"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=220323"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=220323"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=220323"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}