{"id":941929,"date":"2024-12-26T21:48:12","date_gmt":"2024-12-26T13:48:12","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/941929.html"},"modified":"2024-12-26T21:48:14","modified_gmt":"2024-12-26T13:48:14","slug":"python%e5%a6%82%e4%bd%95%e5%88%9b%e5%bb%bappt","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/941929.html","title":{"rendered":"python\u5982\u4f55\u521b\u5efappt"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25075943\/21270676-4689-45a9-9109-24f8b3e254f4.webp\" alt=\"python\u5982\u4f55\u521b\u5efappt\" \/><\/p>\n<p><p> <strong>\u8981\u5728Python\u4e2d\u521b\u5efaPPT\uff0c\u53ef\u4ee5\u4f7f\u7528\u5e93\u5982python-pptx\u3001\u81ea\u52a8\u5316\u751f\u6210\u3001\u52a8\u6001\u5185\u5bb9\u63d2\u5165\u3002\u8fd9\u4e9b\u5de5\u5177\u53ef\u4ee5\u5e2e\u52a9\u4f60\u8f7b\u677e\u521b\u5efa\u548c\u64cd\u4f5cPowerPoint\u6587\u4ef6\u3002<\/strong> \u4f7f\u7528python-pptx\uff0c\u4f60\u53ef\u4ee5\u521b\u5efa\u6f14\u793a\u6587\u7a3f\u3001\u6dfb\u52a0\u5e7b\u706f\u7247\u3001\u63d2\u5165\u6587\u672c\u548c\u56fe\u50cf\u7b49\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5229\u7528python-pptx\u5e93\u6765\u521b\u5efa\u548c\u7f16\u8f91PPT\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u5b89\u88c5\u548c\u8bbe\u7f6epython-pptx<\/p>\n<\/p>\n<p><p>\u5728\u5f00\u59cb\u4f7f\u7528python-pptx\u5e93\u4e4b\u524d\uff0c\u4f60\u9700\u8981\u786e\u4fdd\u5b83\u5df2\u7ecf\u5b89\u88c5\u5728\u4f60\u7684Python\u73af\u5883\u4e2d\u3002\u53ef\u4ee5\u901a\u8fc7pip\u547d\u4ee4\u6765\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install python-pptx<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u547d\u4ee4\u5c06\u81ea\u52a8\u4e0b\u8f7d\u5e76\u5b89\u88c5python-pptx\u53ca\u5176\u6240\u6709\u4f9d\u8d56\u9879\u3002\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u4f60\u53ef\u4ee5\u5728Python\u811a\u672c\u4e2d\u5bfc\u5165\u8be5\u5e93\uff0c\u5e76\u5f00\u59cb\u4f7f\u7528\u5176\u529f\u80fd\u6765\u521b\u5efa\u548c\u4fee\u6539PPT\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u521b\u5efa\u4e00\u4e2a\u65b0\u7684PPT\u6587\u4ef6<\/p>\n<\/p>\n<p><p>\u4f7f\u7528python-pptx\u521b\u5efa\u4e00\u4e2a\u65b0\u7684PPT\u6587\u4ef6\u975e\u5e38\u7b80\u5355\u3002\u9996\u5148\uff0c\u4f60\u9700\u8981\u5bfc\u5165\u5e93\uff0c\u5e76\u5b9e\u4f8b\u5316\u4e00\u4e2aPresentation\u5bf9\u8c61\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pptx import Presentation<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u65b0\u7684PPT<\/strong><\/h2>\n<p>presentation = Presentation()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2aPresentation\u5bf9\u8c61\u662fPPT\u6587\u4ef6\u7684\u57fa\u7840\uff0c\u901a\u8fc7\u5b83\u4f60\u53ef\u4ee5\u6dfb\u52a0\u5e7b\u706f\u7247\u3001\u6587\u672c\u3001\u56fe\u7247\u7b49\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u6dfb\u52a0\u5e7b\u706f\u7247<\/p>\n<\/p>\n<p><p>\u5728\u521b\u5efa\u4e86Presentation\u5bf9\u8c61\u540e\uff0c\u63a5\u4e0b\u6765\u5c31\u662f\u5411PPT\u4e2d\u6dfb\u52a0\u5e7b\u706f\u7247\u3002python-pptx\u63d0\u4f9b\u4e86\u591a\u79cd\u5e7b\u706f\u7247\u5e03\u5c40\u53ef\u4f9b\u9009\u62e9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6dfb\u52a0\u4e00\u5f20\u5e7b\u706f\u7247\uff0c\u4f7f\u7528\u6807\u9898\u548c\u5185\u5bb9\u7684\u5e03\u5c40<\/p>\n<p>slide_layout = presentation.slide_layouts[1]  # \u4f7f\u7528\u7b2c\u4e8c\u79cd\u5e03\u5c40<\/p>\n<p>slide = presentation.slides.add_slide(slide_layout)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u91cc\u6211\u4eec\u9009\u62e9\u4e86\u7b2c\u4e8c\u79cd\u5e03\u5c40\uff0c\u8fd9\u79cd\u5e03\u5c40\u901a\u5e38\u5305\u62ec\u4e00\u4e2a\u6807\u9898\u548c\u4e00\u4e2a\u5185\u5bb9\u533a\u57df\u3002\u4f60\u53ef\u4ee5\u6839\u636e\u9700\u8981\u9009\u62e9\u5176\u4ed6\u5e03\u5c40\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u63d2\u5165\u6587\u672c<\/p>\n<\/p>\n<p><p>\u5411\u5e7b\u706f\u7247\u4e2d\u63d2\u5165\u6587\u672c\u662fPPT\u5236\u4f5c\u7684\u91cd\u8981\u90e8\u5206\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u4ee3\u7801\u6dfb\u52a0\u6807\u9898\u548c\u6b63\u6587\u6587\u672c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u83b7\u53d6\u6807\u9898\u548c\u5185\u5bb9\u6846<\/p>\n<p>title = slide.shapes.title<\/p>\n<p>content = slide.placeholders[1]<\/p>\n<h2><strong>\u8bbe\u7f6e\u6807\u9898\u548c\u5185\u5bb9<\/strong><\/h2>\n<p>title.text = &quot;\u8fd9\u662f\u4e00\u4e2a\u6807\u9898&quot;<\/p>\n<p>content.text = &quot;\u8fd9\u662f\u5e7b\u706f\u7247\u7684\u6b63\u6587\u5185\u5bb9\u3002&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u4f60\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06\u6587\u672c\u63d2\u5165\u5230\u5e7b\u706f\u7247\u4e2d\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u63d2\u5165\u56fe\u7247<\/p>\n<\/p>\n<p><p>\u9664\u4e86\u6587\u672c\u5916\uff0c\u56fe\u7247\u4e5f\u662fPPT\u4e2d\u5e38\u89c1\u7684\u5143\u7d20\u3002\u4f7f\u7528python-pptx\uff0c\u4f60\u53ef\u4ee5\u5728\u5e7b\u706f\u7247\u4e2d\u63d2\u5165\u56fe\u50cf\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pptx.util import Inches<\/p>\n<h2><strong>\u63d2\u5165\u56fe\u7247<\/strong><\/h2>\n<p>img_path = &#39;path_to_image.jpg&#39;<\/p>\n<p>slide.shapes.add_picture(img_path, Inches(1), Inches(1), width=Inches(5), height=Inches(5))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u5c06\u4e00\u5f20\u56fe\u7247\u63d2\u5165\u5230\u5e7b\u706f\u7247\u4e2d\uff0c\u6307\u5b9a\u4e86\u4f4d\u7f6e\u548c\u5927\u5c0f\u3002<\/p>\n<\/p>\n<p><p>\u516d\u3001\u4fdd\u5b58PPT\u6587\u4ef6<\/p>\n<\/p>\n<p><p>\u5b8c\u6210\u6240\u6709\u4fee\u6539\u540e\uff0c\u4f60\u9700\u8981\u5c06PPT\u4fdd\u5b58\u5230\u6587\u4ef6\u4e2d\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4fdd\u5b58PPT\u6587\u4ef6<\/p>\n<p>presentation.save(&#39;new_presentation.pptx&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6837\u5c31\u53ef\u4ee5\u5c06\u4f60\u521b\u5efa\u7684PPT\u4fdd\u5b58\u5230\u6307\u5b9a\u7684\u8def\u5f84\u3002<\/p>\n<\/p>\n<p><p>\u4e03\u3001\u5e94\u7528\u573a\u666f\u548c\u6ce8\u610f\u4e8b\u9879<\/p>\n<\/p>\n<p><p>python-pptx\u5e93\u975e\u5e38\u9002\u5408\u7528\u4e8e\u81ea\u52a8\u5316\u62a5\u544a\u751f\u6210\u3001\u6279\u91cf\u5e7b\u706f\u7247\u5236\u4f5c\u3001\u52a8\u6001\u5185\u5bb9\u5c55\u793a\u7b49\u573a\u666f\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u8be5\u5e93\u76ee\u524d\u4e0d\u652f\u6301\u4e00\u4e9b\u9ad8\u7ea7\u529f\u80fd\uff0c\u5982\u52a8\u753b\u548c\u590d\u6742\u7684\u683c\u5f0f\u8bbe\u7f6e\uff0c\u56e0\u6b64\u5728\u4f7f\u7528\u65f6\u9700\u8981\u6839\u636e\u9700\u6c42\u8fdb\u884c\u53d6\u820d\u3002<\/p>\n<\/p>\n<p><p>\u603b\u7ed3\u6765\u8bf4\uff0cpython-pptx\u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u4e14\u6613\u4e8e\u4f7f\u7528\u7684\u5de5\u5177\uff0c\u80fd\u591f\u6781\u5927\u5730\u7b80\u5316PPT\u7684\u521b\u5efa\u548c\u4fee\u6539\u8fc7\u7a0b\u3002\u901a\u8fc7\u5bf9\u5176API\u7684\u719f\u7ec3\u638c\u63e1\uff0c\u53ef\u4ee5\u5c06\u590d\u6742\u7684PPT\u5236\u4f5c\u6d41\u7a0b\u81ea\u52a8\u5316\uff0c\u63d0\u9ad8\u5de5\u4f5c\u6548\u7387\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u521b\u5efaPPT\u6587\u4ef6\uff1f<\/strong><br \/>Python\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u5e93\u6765\u521b\u5efaPPT\u6587\u4ef6\uff0c\u5176\u4e2d\u6700\u6d41\u884c\u7684\u662f<code>python-pptx<\/code>\u3002\u8fd9\u4e2a\u5e93\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u7b80\u5355\u6613\u7528\u7684\u63a5\u53e3\uff0c\u53ef\u4ee5\u7528\u6765\u751f\u6210\u548c\u4fee\u6539PPT\u5e7b\u706f\u7247\u3002\u7528\u6237\u53ea\u9700\u5b89\u88c5\u8be5\u5e93\uff0c\u5e76\u901a\u8fc7\u7f16\u5199Python\u4ee3\u7801\u6765\u521b\u5efa\u5e7b\u706f\u7247\u3001\u6dfb\u52a0\u6587\u672c\u3001\u63d2\u5165\u56fe\u7247\u7b49\u3002<\/p>\n<p><strong>\u4f7f\u7528Python\u521b\u5efaPPT\u9700\u8981\u54ea\u4e9b\u57fa\u672c\u6b65\u9aa4\uff1f<\/strong><br \/>\u521b\u5efaPPT\u7684\u57fa\u672c\u6b65\u9aa4\u5305\u62ec\uff1a\u9996\u5148\uff0c\u5b89\u88c5<code>python-pptx<\/code>\u5e93\u3002\u63a5\u7740\uff0c\u5bfc\u5165\u5e93\u5e76\u521b\u5efa\u4e00\u4e2aPresentation\u5bf9\u8c61\u3002\u7136\u540e\uff0c\u53ef\u4ee5\u6dfb\u52a0\u5e7b\u706f\u7247\uff0c\u5e76\u5728\u6bcf\u4e2a\u5e7b\u706f\u7247\u4e0a\u63d2\u5165\u6587\u672c\u6846\u3001\u6807\u9898\u3001\u56fe\u7247\u7b49\u5143\u7d20\u3002\u6700\u540e\uff0c\u901a\u8fc7\u8c03\u7528\u4fdd\u5b58\u65b9\u6cd5\u5c06\u521b\u5efa\u7684PPT\u6587\u4ef6\u4fdd\u5b58\u5230\u672c\u5730\u3002<\/p>\n<p><strong>\u521b\u5efaPPT\u65f6\uff0c\u5982\u4f55\u6dfb\u52a0\u56fe\u7247\u548c\u56fe\u8868\uff1f<\/strong><br \/>\u5728\u4f7f\u7528<code>python-pptx<\/code>\u521b\u5efaPPT\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7<code>Slide.shapes.add_picture()<\/code>\u65b9\u6cd5\u6765\u6dfb\u52a0\u56fe\u7247\u3002\u7528\u6237\u53ea\u9700\u63d0\u4f9b\u56fe\u7247\u6587\u4ef6\u7684\u8def\u5f84\u548c\u63d2\u5165\u4f4d\u7f6e\u7684\u5750\u6807\u3002\u5bf9\u4e8e\u56fe\u8868\uff0c\u53ef\u4ee5\u5229\u7528\u5176\u4ed6\u5e93\u5982<code>matplotlib<\/code>\u751f\u6210\u56fe\u8868\u540e\uff0c\u5c06\u5176\u4fdd\u5b58\u4e3a\u56fe\u7247\u683c\u5f0f\uff0c\u7136\u540e\u540c\u6837\u4f7f\u7528\u4e0a\u8ff0\u65b9\u6cd5\u5c06\u5176\u63d2\u5165\u5230PPT\u4e2d\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u5728Python\u4e2d\u521b\u5efaPPT\uff0c\u53ef\u4ee5\u4f7f\u7528\u5e93\u5982python-pptx\u3001\u81ea\u52a8\u5316\u751f\u6210\u3001\u52a8\u6001\u5185\u5bb9\u63d2\u5165\u3002\u8fd9\u4e9b\u5de5\u5177\u53ef\u4ee5\u5e2e\u52a9\u4f60 [&hellip;]","protected":false},"author":3,"featured_media":941932,"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\/941929"}],"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=941929"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/941929\/revisions"}],"predecessor-version":[{"id":941934,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/941929\/revisions\/941934"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/941932"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=941929"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=941929"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=941929"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}