{"id":1182143,"date":"2025-01-15T18:59:45","date_gmt":"2025-01-15T10:59:45","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1182143.html"},"modified":"2025-01-15T18:59:47","modified_gmt":"2025-01-15T10:59:47","slug":"%e5%a6%82%e4%bd%95%e5%b0%86python%e6%96%87%e4%bb%b6pdf","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1182143.html","title":{"rendered":"\u5982\u4f55\u5c06Python\u6587\u4ef6PDF"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25130241\/b8e78228-be16-4d20-b384-6b14b19ca280.webp\" alt=\"\u5982\u4f55\u5c06Python\u6587\u4ef6PDF\" \/><\/p>\n<p><p> <strong>\u5c06Python\u6587\u4ef6\u8f6c\u6362\u4e3aPDF\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u5982ReportLab\u3001PDFKit\u3001\u4ee5\u53ca\u901a\u8fc7LaTeX\u8fdb\u884c\u8f6c\u6362\u7b49\u3002<\/strong>\u5176\u4e2d\uff0c\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u7684\u65b9\u6cd5\u6bd4\u8f83\u7b80\u5355\u4e14\u76f4\u63a5\uff0c\u9002\u5408\u5927\u591a\u6570\u7528\u6237\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5176\u4e2d\u4e00\u79cd\u65b9\u6cd5\uff0c\u5373\u4f7f\u7528ReportLab\u5e93\u5c06Python\u6587\u4ef6\u8f6c\u6362\u4e3aPDF\u7684\u8fc7\u7a0b\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b89\u88c5ReportLab\u5e93<\/h3>\n<\/p>\n<p><p>ReportLab\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u5e93\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u8f7b\u677e\u5730\u751f\u6210PDF\u6587\u4ef6\u3002\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5b89\u88c5ReportLab\u5e93\u3002\u5728\u547d\u4ee4\u884c\u6216\u7ec8\u7aef\u4e2d\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install reportlab<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528ReportLab\u751f\u6210PDF\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u5f00\u59cb\u7f16\u5199\u4ee3\u7801\u5c06Python\u6587\u4ef6\u8f6c\u6362\u4e3aPDF\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u57fa\u672c\u7684\u793a\u4f8b\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_pdf(filename):<\/p>\n<p>    # \u521b\u5efa\u4e00\u4e2aCanvas\u5bf9\u8c61<\/p>\n<p>    c = canvas.Canvas(filename, pagesize=letter)<\/p>\n<p>    width, height = letter<\/p>\n<p>    # \u8bbe\u7f6e\u5b57\u4f53<\/p>\n<p>    c.setFont(&quot;Helvetica&quot;, 12)<\/p>\n<p>    # \u6253\u5f00Python\u6587\u4ef6\u5e76\u8bfb\u53d6\u5185\u5bb9<\/p>\n<p>    with open(&quot;example.py&quot;, &quot;r&quot;) as file:<\/p>\n<p>        lines = file.readlines()<\/p>\n<p>    # \u5b9a\u4e49\u521d\u59cb\u5750\u6807<\/p>\n<p>    x = 50<\/p>\n<p>    y = height - 50<\/p>\n<p>    # \u5c06\u6bcf\u4e00\u884c\u5185\u5bb9\u5199\u5165PDF<\/p>\n<p>    for line in lines:<\/p>\n<p>        if y &lt; 50:<\/p>\n<p>            c.showPage()<\/p>\n<p>            c.setFont(&quot;Helvetica&quot;, 12)<\/p>\n<p>            y = height - 50<\/p>\n<p>        c.drawString(x, y, line.strip())<\/p>\n<p>        y -= 15<\/p>\n<p>    # \u4fdd\u5b58PDF\u6587\u4ef6<\/p>\n<p>    c.save()<\/p>\n<p>create_pdf(&quot;output.pdf&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u89e3\u91ca\u4ee3\u7801<\/h3>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5bfc\u5165\u5fc5\u8981\u7684\u6a21\u5757<\/strong>\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><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u521b\u5efaCanvas\u5bf9\u8c61<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">c = canvas.Canvas(filename, pagesize=letter)<\/p>\n<p>width, height = letter<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u91cc\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2aCanvas\u5bf9\u8c61\uff0c\u5e76\u8bbe\u7f6e\u9875\u9762\u5927\u5c0f\u4e3aletter\u5c3a\u5bf8\uff088.5 x 11\u82f1\u5bf8\uff09\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8bbe\u7f6e\u5b57\u4f53<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">c.setFont(&quot;Helvetica&quot;, 12)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u9009\u62e9Helvetica\u5b57\u4f53\uff0c\u5b57\u53f7\u4e3a12\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8bfb\u53d6Python\u6587\u4ef6\u5185\u5bb9<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&quot;example.py&quot;, &quot;r&quot;) as file:<\/p>\n<p>    lines = file.readlines()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6253\u5f00\u540d\u4e3a<code>example.py<\/code>\u7684Python\u6587\u4ef6\uff0c\u5e76\u8bfb\u53d6\u6240\u6709\u884c\u5230<code>lines<\/code>\u5217\u8868\u4e2d\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5b9a\u4e49\u521d\u59cb\u5750\u6807<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">x = 50<\/p>\n<p>y = height - 50<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8bbe\u7f6e\u521d\u59cb\u5750\u6807<code>x<\/code>\u548c<code>y<\/code>\uff0c\u4ee5\u4fbf\u4ece\u9875\u9762\u9876\u90e8\u5f00\u59cb\u5199\u5165\u5185\u5bb9\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5c06\u5185\u5bb9\u5199\u5165PDF<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for line in lines:<\/p>\n<p>    if y &lt; 50:<\/p>\n<p>        c.showPage()<\/p>\n<p>        c.setFont(&quot;Helvetica&quot;, 12)<\/p>\n<p>        y = height - 50<\/p>\n<p>    c.drawString(x, y, line.strip())<\/p>\n<p>    y -= 15<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u904d\u5386\u6bcf\u4e00\u884c\u5185\u5bb9\uff0c\u5c06\u5176\u5199\u5165PDF\u6587\u4ef6\u3002\u5982\u679c\u5f53\u524d\u9875\u9762\u5199\u6ee1\uff0c\u5219\u65b0\u5efa\u4e00\u4e2a\u9875\u9762\u5e76\u7ee7\u7eed\u5199\u5165\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4fdd\u5b58PDF\u6587\u4ef6<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">c.save()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4fdd\u5b58\u751f\u6210\u7684PDF\u6587\u4ef6\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u56db\u3001\u5176\u4ed6\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u4f7f\u7528ReportLab\u5e93\uff0c\u8fd8\u6709\u4e00\u4e9b\u5176\u4ed6\u65b9\u6cd5\u53ef\u4ee5\u5c06Python\u6587\u4ef6\u8f6c\u6362\u4e3aPDF\u3002\u4f8b\u5982\uff0c\u4f7f\u7528PDFKit\u5e93\u6216\u8005\u901a\u8fc7LaTeX\u8fdb\u884c\u8f6c\u6362\u3002\u4e0b\u9762\u7b80\u8981\u4ecb\u7ecd\u8fd9\u4e24\u79cd\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4f7f\u7528PDFKit\u5e93<\/h3>\n<\/p>\n<p><p>PDFKit\u662f\u4e00\u4e2a\u7b80\u5355\u7684Python\u5e93\uff0c\u53ef\u4ee5\u5c06HTML\u548cURL\u8f6c\u6362\u4e3aPDF\u3002\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5b89\u88c5PDFKit\u548cwkhtmltopdf\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pdfkit<\/p>\n<p>sudo apt-get install wkhtmltopdf<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u7f16\u5199\u4ee5\u4e0b\u4ee3\u7801\u5c06Python\u6587\u4ef6\u8f6c\u6362\u4e3aPDF\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pdfkit<\/p>\n<h2><strong>\u8bfb\u53d6Python\u6587\u4ef6\u5185\u5bb9\u5e76\u8f6c\u6362\u4e3aHTML\u683c\u5f0f<\/strong><\/h2>\n<p>with open(&quot;example.py&quot;, &quot;r&quot;) as file:<\/p>\n<p>    content = file.read()<\/p>\n<p>html_content = f&quot;&lt;pre&gt;{content}&lt;\/pre&gt;&quot;<\/p>\n<h2><strong>\u5c06HTML\u5185\u5bb9\u4fdd\u5b58\u4e3a\u4e34\u65f6\u6587\u4ef6<\/strong><\/h2>\n<p>with open(&quot;temp.html&quot;, &quot;w&quot;) as file:<\/p>\n<p>    file.write(html_content)<\/p>\n<h2><strong>\u4f7f\u7528PDFKit\u5c06HTML\u6587\u4ef6\u8f6c\u6362\u4e3aPDF<\/strong><\/h2>\n<p>pdfkit.from_file(&quot;temp.html&quot;, &quot;output.pdf&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u901a\u8fc7LaTeX\u8fdb\u884c\u8f6c\u6362<\/h3>\n<\/p>\n<p><p>LaTeX\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u6392\u7248\u7cfb\u7edf\uff0c\u9002\u5408\u751f\u6210\u590d\u6742\u7684PDF\u6587\u6863\u3002\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5b89\u88c5LaTeX\u53d1\u884c\u7248\uff0c\u5982TeX Live\u6216\u8005MiKTeX\u3002\u7136\u540e\uff0c\u7f16\u5199\u4e00\u4e2aLaTeX\u6a21\u677f\uff0c\u5c06Python\u4ee3\u7801\u5d4c\u5165\u5176\u4e2d\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-latex\">\\documentclass{article}<\/p>\n<p>\\usepackage{listings}<\/p>\n<p>\\begin{document}<\/p>\n<p>\\lstset{language=Python}<\/p>\n<p>\\lstinputlisting{example.py}<\/p>\n<p>\\end{document}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4fdd\u5b58\u4e3a<code>template.tex<\/code>\u6587\u4ef6\u3002\u7136\u540e\uff0c\u4f7f\u7528\u547d\u4ee4\u884c\u5c06\u5176\u8f6c\u6362\u4e3aPDF\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pdflatex template.tex<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p><strong>\u5c06Python\u6587\u4ef6\u8f6c\u6362\u4e3aPDF\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528ReportLab\u3001PDFKit\u3001\u4ee5\u53ca\u901a\u8fc7LaTeX\u8fdb\u884c\u8f6c\u6362\u3002<\/strong>\u672c\u6587\u8be6\u7ec6\u4ecb\u7ecd\u4e86\u4f7f\u7528ReportLab\u5e93\u7684\u65b9\u6cd5\uff0c\u5e76\u7b80\u8981\u4ecb\u7ecd\u4e86\u5176\u4ed6\u4e24\u79cd\u65b9\u6cd5\u3002\u9009\u62e9\u9002\u5408\u81ea\u5df1\u7684\u65b9\u6cd5\u53ef\u4ee5\u66f4\u9ad8\u6548\u5730\u5b8c\u6210\u4efb\u52a1\u3002\u5982\u679c\u9700\u8981\u751f\u6210\u590d\u6742\u7684PDF\u6587\u6863\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528LaTeX\uff1b\u5982\u679c\u9700\u8981\u5feb\u901f\u751f\u6210PDF\u6587\u4ef6\uff0c\u53ef\u4ee5\u9009\u62e9ReportLab\u6216PDFKit\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5c06Python\u6587\u4ef6\u8f6c\u6362\u4e3aPDF\u683c\u5f0f\uff1f<\/strong><br \/>\u5c06Python\u6587\u4ef6\u8f6c\u6362\u4e3aPDF\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5b9e\u73b0\uff0c\u5e38\u89c1\u7684\u65b9\u5f0f\u5305\u62ec\u4f7f\u7528\u4e13\u95e8\u7684\u5e93\uff0c\u5982ReportLab\u3001FPDF\u6216Pandas\uff08\u7528\u4e8e\u6570\u636e\u8868\u683c\uff09\u3002\u8fd9\u4e9b\u5e93\u5141\u8bb8\u5f00\u53d1\u8005\u521b\u5efa\u548c\u5b9a\u5236PDF\u6587\u6863\u3002\u9996\u5148\uff0c\u60a8\u53ef\u4ee5\u5b89\u88c5\u6240\u9700\u7684\u5e93\uff0c\u4f8b\u5982\u4f7f\u7528<code>pip install reportlab<\/code>\u3002\u63a5\u7740\uff0c\u901a\u8fc7\u7f16\u5199\u811a\u672c\uff0c\u5c06Python\u4e2d\u7684\u5185\u5bb9\u683c\u5f0f\u5316\u5e76\u8f93\u51fa\u4e3aPDF\u6587\u4ef6\u3002\u5177\u4f53\u7684\u5b9e\u73b0\u65b9\u5f0f\u4f1a\u6839\u636e\u60a8\u60f3\u8981\u751f\u6210\u7684PDF\u5185\u5bb9\u800c\u6709\u6240\u4e0d\u540c\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u4f7f\u7528\u5728\u7ebf\u5de5\u5177\u5c06Python\u6587\u4ef6\u8f6c\u6362\u4e3aPDF\uff1f<\/strong><br \/>\u786e\u5b9e\u6709\u8bb8\u591a\u5728\u7ebf\u5de5\u5177\u53ef\u4ee5\u5c06Python\u6587\u4ef6\u8f6c\u6362\u4e3aPDF\u683c\u5f0f\u3002\u8fd9\u4e9b\u5de5\u5177\u901a\u5e38\u652f\u6301\u4e0a\u4f20\u6587\u4ef6\uff0c\u7136\u540e\u81ea\u52a8\u751f\u6210PDF\u6587\u6863\u3002\u4f7f\u7528\u5728\u7ebf\u5de5\u5177\u7684\u597d\u5904\u5728\u4e8e\u65e0\u9700\u5b89\u88c5\u4efb\u4f55\u8f6f\u4ef6\uff0c\u4f46\u662f\u8bf7\u6ce8\u610f\u4fdd\u62a4\u60a8\u7684\u4ee3\u7801\u9690\u79c1\uff0c\u56e0\u4e3a\u4e0a\u4f20\u6587\u4ef6\u53ef\u80fd\u4f1a\u6d89\u53ca\u5230\u5b89\u5168\u98ce\u9669\u3002\u9009\u62e9\u4fe1\u8a89\u826f\u597d\u7684\u5728\u7ebf\u670d\u52a1\u53ef\u4ee5\u964d\u4f4e\u8fd9\u4e9b\u98ce\u9669\u3002<\/p>\n<p><strong>\u8f6c\u6362\u540e\u7684PDF\u6587\u4ef6\u662f\u5426\u53ef\u4ee5\u7f16\u8f91\uff1f<\/strong><br \/>\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u4f7f\u7528Python\u751f\u6210\u7684PDF\u6587\u4ef6\u662f\u4e0d\u53ef\u7f16\u8f91\u7684\uff0c\u5c24\u5176\u662f\u5982\u679c\u60a8\u4f7f\u7528\u4e86\u6807\u51c6\u5e93\u751f\u6210\u9759\u6001\u6587\u6863\u3002\u4e0d\u8fc7\uff0c\u6709\u4e00\u4e9b\u5de5\u5177\u548c\u8f6f\u4ef6\u53ef\u4ee5\u7f16\u8f91PDF\u6587\u4ef6\uff0c\u4f8b\u5982Adobe Acrobat\u6216\u4e00\u4e9b\u514d\u8d39\u7684PDF\u7f16\u8f91\u5668\u3002\u5982\u679c\u60a8\u9700\u8981\u5728\u751f\u6210\u7684PDF\u4e2d\u6dfb\u52a0\u53ef\u7f16\u8f91\u7684\u5185\u5bb9\uff0c\u5efa\u8bae\u4f7f\u7528\u76f8\u5e94\u7684\u5e93\u6765\u521b\u5efa\u53ef\u586b\u5145\u7684\u8868\u5355\u5b57\u6bb5\uff0c\u8fd9\u6837\u7528\u6237\u53ef\u4ee5\u5728PDF\u4e2d\u8f93\u5165\u4fe1\u606f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5c06Python\u6587\u4ef6\u8f6c\u6362\u4e3aPDF\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u5982ReportLab\u3001PDFKit\u3001\u4ee5\u53ca\u901a\u8fc7LaT [&hellip;]","protected":false},"author":3,"featured_media":1182149,"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\/1182143"}],"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=1182143"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1182143\/revisions"}],"predecessor-version":[{"id":1182151,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1182143\/revisions\/1182151"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1182149"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1182143"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1182143"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1182143"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}