{"id":1000060,"date":"2024-12-27T09:46:48","date_gmt":"2024-12-27T01:46:48","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1000060.html"},"modified":"2024-12-27T09:46:53","modified_gmt":"2024-12-27T01:46:53","slug":"python%e4%bb%a3%e7%a0%81%e5%a6%82%e4%bd%95%e5%af%bc%e5%87%bapdf","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1000060.html","title":{"rendered":"Python\u4ee3\u7801\u5982\u4f55\u5bfc\u51fapdf"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25074823\/ca0fa10f-e8fb-4c10-9260-2273501d0027.webp\" alt=\"Python\u4ee3\u7801\u5982\u4f55\u5bfc\u51fapdf\" \/><\/p>\n<p><p> <strong>Python\u4ee3\u7801\u5bfc\u51faPDF\u7684\u65b9\u6cd5\u4e3b\u8981\u5305\u62ec\uff1a\u4f7f\u7528ReportLab\u5e93\u3001\u7ed3\u5408FPDF\u548cPandas\u5e93\u3001\u5229\u7528WeasyPrint\u8fdb\u884cHTML\u5230PDF\u8f6c\u6362\u3002\u4f7f\u7528ReportLab\u5e93\u662f\u4e00\u79cd\u5f3a\u5927\u4e14\u7075\u6d3b\u7684\u65b9\u5f0f\uff0c\u56e0\u4e3a\u5b83\u5141\u8bb8\u7528\u6237\u521b\u5efa\u590d\u6742\u7684PDF\u6587\u6863\u3002<\/strong><\/p>\n<\/p>\n<p><p>ReportLab\u5e93\u662fPython\u4e2d\u6700\u5e38\u7528\u7684\u521b\u5efaPDF\u7684\u5e93\u4e4b\u4e00\uff0c\u63d0\u4f9b\u4e86\u975e\u5e38\u7075\u6d3b\u7684\u529f\u80fd\uff0c\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u8005\u751f\u6210\u5404\u79cd\u6837\u5f0f\u7684PDF\u6587\u6863\u3002\u901a\u8fc7ReportLab\uff0c\u4f60\u53ef\u4ee5\u63a7\u5236PDF\u7684\u6bcf\u4e00\u4e2a\u7ec6\u8282\uff0c\u5982\u5b57\u4f53\u3001\u989c\u8272\u3001\u5e03\u5c40\u7b49\uff0c\u9002\u7528\u4e8e\u9700\u8981\u81ea\u5b9a\u4e49\u683c\u5f0f\u7684\u590d\u6742\u6587\u6863\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528ReportLab\u5e93\u6765\u5bfc\u51faPDF\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528ReportLab\u5e93<\/p>\n<\/p>\n<ol>\n<li>\u5b89\u88c5\u4e0e\u57fa\u672c\u4f7f\u7528<\/li>\n<\/ol>\n<p><p>\u9996\u5148\u9700\u8981\u5b89\u88c5ReportLab\u5e93\uff0c\u53ef\u4ee5\u901a\u8fc7pip\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install reportlab<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u7b80\u5355\u4ee3\u7801\u751f\u6210\u4e00\u4e2aPDF\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from reportlab.pdfgen import canvas<\/p>\n<p>def create_pdf(filename):<\/p>\n<p>    c = canvas.Canvas(filename)<\/p>\n<p>    c.drawString(100, 750, &quot;Welcome to ReportLab!&quot;)<\/p>\n<p>    c.save()<\/p>\n<p>create_pdf(&quot;example.pdf&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u521b\u5efa\u4e86\u4e00\u4e2a\u7b80\u5355\u7684PDF\u6587\u4ef6\uff0c\u5305\u542b\u4e86\u4e00\u884c\u6587\u672c\u201cWelcome to ReportLab!\u201d\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li>\u8bbe\u7f6e\u9875\u9762\u683c\u5f0f\u4e0e\u6dfb\u52a0\u5143\u7d20<\/li>\n<\/ol>\n<p><p>ReportLab\u5141\u8bb8\u7528\u6237\u8bbe\u7f6e\u9875\u9762\u5c3a\u5bf8\u3001\u8fb9\u8ddd\u3001\u5b57\u4f53\u7b49\u5404\u79cd\u683c\u5f0f\uff0c\u540c\u65f6\u8fd8\u53ef\u4ee5\u6dfb\u52a0\u56fe\u50cf\u3001\u8868\u683c\u548c\u56fe\u5f62\u5143\u7d20\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u7528\u64cd\u4f5c\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u8bbe\u7f6e\u9875\u9762\u5c3a\u5bf8\u548c\u8fb9\u8ddd\uff1a<\/li>\n<\/ul>\n<p><pre><code class=\"language-python\">from reportlab.lib.pagesizes import letter<\/p>\n<p>c = canvas.Canvas(&quot;example.pdf&quot;, pagesize=letter)<\/p>\n<p>width, height = letter<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ul>\n<li>\u6dfb\u52a0\u6587\u672c\uff1a<\/li>\n<\/ul>\n<p><pre><code class=\"language-python\">c.setFont(&quot;Helvetica&quot;, 12)<\/p>\n<p>c.drawString(100, height - 100, &quot;This is a line of text.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ul>\n<li>\u6dfb\u52a0\u56fe\u7247\uff1a<\/li>\n<\/ul>\n<p><pre><code class=\"language-python\">c.drawImage(&quot;image.jpg&quot;, 100, height - 200, width=150, height=150)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ul>\n<li>\u7ed8\u5236\u5f62\u72b6\uff1a<\/li>\n<\/ul>\n<p><pre><code class=\"language-python\">c.rect(100, height - 300, 100, 50, fill=1)<\/p>\n<p>c.circle(200, height - 300, 50, fill=0)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li>\u521b\u5efa\u590d\u6742\u5e03\u5c40<\/li>\n<\/ol>\n<p><p>\u5bf9\u4e8e\u66f4\u590d\u6742\u7684\u5e03\u5c40\uff0c\u53ef\u4ee5\u4f7f\u7528ReportLab\u7684Platypus\u6846\u67b6\u3002Platypus\u63d0\u4f9b\u4e86\u66f4\u9ad8\u5c42\u6b21\u7684\u5e03\u5c40\u63a7\u5236\uff0c\u5141\u8bb8\u7528\u6237\u521b\u5efa\u5305\u542b\u591a\u4e2a\u6bb5\u843d\u3001\u56fe\u7247\u548c\u8868\u683c\u7684\u6587\u6863\u3002\u4ee5\u4e0b\u793a\u4f8b\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528Platypus\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u6bb5\u843d\u548c\u56fe\u7247\u7684PDF\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from reportlab.platypus import SimpleDocTemplate, Paragraph, Image<\/p>\n<p>from reportlab.lib.styles import getSampleStyleSheet<\/p>\n<p>def create_complex_pdf(filename):<\/p>\n<p>    doc = SimpleDocTemplate(filename)<\/p>\n<p>    styles = getSampleStyleSheet()<\/p>\n<p>    flowables = []<\/p>\n<p>    paragraph = Paragraph(&quot;This is a paragraph.&quot;, styles[&#39;Normal&#39;])<\/p>\n<p>    flowables.append(paragraph)<\/p>\n<p>    image = Image(&quot;image.jpg&quot;, width=150, height=150)<\/p>\n<p>    flowables.append(image)<\/p>\n<p>    doc.build(flowables)<\/p>\n<p>create_complex_pdf(&quot;complex_example.pdf&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001\u7ed3\u5408FPDF\u548cPandas\u5e93<\/p>\n<\/p>\n<ol>\n<li>\u5b89\u88c5FPDF<\/li>\n<\/ol>\n<p><p>FPDF\u662f\u4e00\u4e2a\u8f7b\u91cf\u7ea7\u7684PDF\u751f\u6210\u5e93\uff0c\u6613\u4e8e\u4f7f\u7528\u4e14\u652f\u6301\u591a\u79cd\u529f\u80fd\u3002\u9996\u5148\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<ol start=\"2\">\n<li>\u5bfc\u51fa\u8868\u683c\u6570\u636e<\/li>\n<\/ol>\n<p><p>FPDF\u53ef\u4ee5\u4e0ePandas\u5e93\u7ed3\u5408\u4f7f\u7528\uff0c\u5c06DataFrame\u6570\u636e\u76f4\u63a5\u5bfc\u51fa\u4e3aPDF\u683c\u5f0f\u3002\u4ee5\u4e0b\u793a\u4f8b\u5c55\u793a\u4e86\u5982\u4f55\u5c06Pandas DataFrame\u5bfc\u51fa\u4e3aPDF\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>from fpdf import FPDF<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u793a\u4f8bDataFrame<\/strong><\/h2>\n<p>data = {&#39;Name&#39;: [&#39;Alice&#39;, &#39;Bob&#39;, &#39;Charlie&#39;], &#39;Age&#39;: [24, 27, 22]}<\/p>\n<p>df = pd.DataFrame(data)<\/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;Title&#39;, 0, 1, &#39;C&#39;)<\/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;, 0, 0, &#39;C&#39;)<\/p>\n<p>    def table(self, df):<\/p>\n<p>        self.set_font(&#39;Arial&#39;, &#39;&#39;, 12)<\/p>\n<p>        for row in df.itertuples():<\/p>\n<p>            self.cell(0, 10, f&#39;{row.Name} {row.Age}&#39;, 0, 1)<\/p>\n<p>pdf = PDF()<\/p>\n<p>pdf.add_page()<\/p>\n<p>pdf.table(df)<\/p>\n<p>pdf.output(&#39;table_example.pdf&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u5229\u7528WeasyPrint\u8fdb\u884cHTML\u5230PDF\u8f6c\u6362<\/p>\n<\/p>\n<ol>\n<li>\u5b89\u88c5WeasyPrint<\/li>\n<\/ol>\n<p><p>WeasyPrint\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u5c06HTML\u6587\u4ef6\u8f6c\u6362\u4e3aPDF\u3002\u5b89\u88c5WeasyPrint\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install weasyprint<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u5c06HTML\u8f6c\u6362\u4e3aPDF<\/li>\n<\/ol>\n<p><p>\u53ef\u4ee5\u4f7f\u7528WeasyPrint\u5c06\u73b0\u6709\u7684HTML\u6587\u4ef6\u5feb\u901f\u8f6c\u6362\u4e3aPDF\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from weasyprint import HTML<\/p>\n<p>HTML(&#39;http:\/\/example.com&#39;).write_pdf(&#39;output.pdf&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6216\u8005\u5c06\u672c\u5730HTML\u6587\u4ef6\u8f6c\u6362\u4e3aPDF\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">HTML(&#39;example.html&#39;).write_pdf(&#39;output.pdf&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u603b\u7ed3\uff1a<\/strong>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u5bfc\u51faPDF\u6587\u4ef6\uff0c\u9009\u62e9\u9002\u5408\u7684\u65b9\u6cd5\u53d6\u51b3\u4e8e\u9879\u76ee\u7684\u5177\u4f53\u9700\u6c42\u3002\u5bf9\u4e8e\u590d\u6742\u7684PDF\u6587\u6863\uff0cReportLab\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u5de5\u5177\uff1b\u5bf9\u4e8e\u7b80\u5355\u7684\u8868\u683c\u5bfc\u51fa\uff0cFPDF\u7ed3\u5408Pandas\u662f\u4e00\u4e2a\u4e0d\u9519\u7684\u9009\u62e9\uff1b\u800c\u5bf9\u4e8e\u5df2\u6709HTML\u5185\u5bb9\u7684PDF\u8f6c\u6362\uff0cWeasyPrint\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u89e3\u51b3\u65b9\u6848\u3002\u901a\u8fc7\u8fd9\u4e9b\u5de5\u5177\uff0c\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0Python\u4ee3\u7801\u5bfc\u51faPDF\u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u5c06\u6570\u636e\u5bfc\u51fa\u4e3aPDF\u6587\u4ef6\uff1f<\/strong><br \/>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u5e93\u6765\u5e2e\u52a9\u7528\u6237\u5c06\u6570\u636e\u5bfc\u51fa\u4e3aPDF\u6587\u4ef6\u3002\u6700\u5e38\u7528\u7684\u5e93\u4e4b\u4e00\u662fReportLab\uff0c\u5b83\u5141\u8bb8\u7528\u6237\u521b\u5efa\u590d\u6742\u7684PDF\u6587\u6863\uff0c\u5305\u62ec\u6587\u672c\u3001\u56fe\u5f62\u548c\u56fe\u50cf\u3002\u7528\u6237\u53ef\u4ee5\u901a\u8fc7\u5b89\u88c5ReportLab\u5e93\uff0c\u5e76\u4f7f\u7528\u5176API\u6765\u5b9a\u4e49PDF\u7684\u7ed3\u6784\u548c\u5185\u5bb9\u3002\u53e6\u4e00\u4e2a\u6d41\u884c\u7684\u9009\u62e9\u662fFPDF\u5e93\uff0c\u5b83\u7b80\u5355\u6613\u7528\uff0c\u9002\u5408\u5feb\u901f\u751f\u6210PDF\u6587\u6863\u3002\u6b64\u5916\uff0cPandas\u5e93\u4e5f\u53ef\u4ee5\u4e0eMatplotlib\u7ed3\u5408\u4f7f\u7528\uff0c\u5c06\u6570\u636e\u53ef\u89c6\u5316\u540e\u5bfc\u51fa\u4e3aPDF\u3002<\/p>\n<p><strong>\u6211\u53ef\u4ee5\u4f7f\u7528\u54ea\u4e9bPython\u5e93\u6765\u521b\u5efaPDF\u6587\u4ef6\uff1f<\/strong><br \/>\u6709\u591a\u4e2aPython\u5e93\u53ef\u4ee5\u7528\u6765\u521b\u5efaPDF\u6587\u4ef6\uff0c\u5176\u4e2d\u4e00\u4e9b\u6700\u5e38\u7528\u7684\u5305\u62ecReportLab\u3001FPDF\u3001PyPDF2\u548cWeasyPrint\u3002ReportLab\u9002\u5408\u751f\u6210\u590d\u6742\u7684PDF\u6587\u4ef6\uff0cFPDF\u5219\u4ee5\u5176\u7b80\u5355\u6027\u8457\u79f0\uff0c\u9002\u5408\u5feb\u901f\u5f00\u53d1\u3002PyPDF2\u4e3b\u8981\u7528\u4e8e\u5904\u7406\u73b0\u6709\u7684PDF\u6587\u4ef6\uff0c\u6bd4\u5982\u5408\u5e76\u6216\u62c6\u5206\uff0c\u800cWeasyPrint\u5219\u53ef\u4ee5\u5c06HTML\u548cCSS\u8f6c\u6362\u6210PDF\uff0c\u9002\u7528\u4e8e\u9700\u8981\u5c06\u7f51\u9875\u5185\u5bb9\u5bfc\u51fa\u4e3aPDF\u7684\u60c5\u51b5\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728\u751f\u6210\u7684PDF\u4e2d\u6dfb\u52a0\u56fe\u50cf\u6216\u56fe\u8868\uff1f<\/strong><br \/>\u5728\u4f7f\u7528Python\u751f\u6210PDF\u65f6\uff0c\u6dfb\u52a0\u56fe\u50cf\u6216\u56fe\u8868\u662f\u4e00\u4e2a\u5e38\u89c1\u9700\u6c42\u3002\u4ee5ReportLab\u4e3a\u4f8b\uff0c\u7528\u6237\u53ef\u4ee5\u4f7f\u7528drawImage()\u65b9\u6cd5\u5c06\u56fe\u50cf\u5d4c\u5165PDF\u4e2d\u3002\u5bf9\u4e8e\u56fe\u8868\uff0c\u7528\u6237\u53ef\u4ee5\u4f7f\u7528Matplotlib\u751f\u6210\u56fe\u5f62\uff0c\u5e76\u5c06\u5176\u4fdd\u5b58\u4e3a\u56fe\u50cf\u6587\u4ef6\uff0c\u7136\u540e\u4f7f\u7528ReportLab\u7684\u56fe\u50cf\u529f\u80fd\u5c06\u5176\u6dfb\u52a0\u5230PDF\u4e2d\u3002FPDF\u4e5f\u652f\u6301\u7c7b\u4f3c\u7684\u529f\u80fd\uff0c\u7528\u6237\u53ef\u4ee5\u4f7f\u7528image()\u65b9\u6cd5\u6765\u63d2\u5165\u56fe\u50cf\u3002\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u7528\u6237\u53ef\u4ee5\u521b\u5efa\u5305\u542b\u4e30\u5bcc\u89c6\u89c9\u5143\u7d20\u7684PDF\u6587\u4ef6\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4ee3\u7801\u5bfc\u51faPDF\u7684\u65b9\u6cd5\u4e3b\u8981\u5305\u62ec\uff1a\u4f7f\u7528ReportLab\u5e93\u3001\u7ed3\u5408FPDF\u548cPandas\u5e93\u3001\u5229\u7528Weas [&hellip;]","protected":false},"author":3,"featured_media":1000083,"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\/1000060"}],"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=1000060"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1000060\/revisions"}],"predecessor-version":[{"id":1000085,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1000060\/revisions\/1000085"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1000083"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1000060"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1000060"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1000060"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}