{"id":974930,"date":"2024-12-27T06:09:12","date_gmt":"2024-12-26T22:09:12","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/974930.html"},"modified":"2024-12-27T06:09:14","modified_gmt":"2024-12-26T22:09:14","slug":"python%e5%a6%82%e4%bd%95%e6%9b%b4%e6%96%b0ppt%e6%96%87%e6%9c%ac","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/974930.html","title":{"rendered":"python\u5982\u4f55\u66f4\u65b0ppt\u6587\u672c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24200418\/cd46707b-e67b-4c8b-97c4-f42eca19c3ee.webp\" alt=\"python\u5982\u4f55\u66f4\u65b0ppt\u6587\u672c\" \/><\/p>\n<p><p> \u4e00\u3001\u4f7f\u7528Python\u66f4\u65b0PPT\u6587\u672c\u7684\u65b9\u6cd5<\/p>\n<\/p>\n<p><p>\u4f7f\u7528Python\u66f4\u65b0PPT\u6587\u672c\u53ef\u4ee5\u901a\u8fc7<strong>python-pptx\u5e93\u3001\u5b9a\u4f4d\u9700\u8981\u4fee\u6539\u7684\u6587\u672c\u6846\u3001\u8fdb\u884c\u6587\u672c\u66ff\u6362\u3001\u4fdd\u5b58\u66f4\u65b0\u540e\u7684PPT\u6587\u4ef6<\/strong>\u6765\u5b9e\u73b0\u3002\u5176\u4e2d\uff0c\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u5229\u7528python-pptx\u5e93\u6765\u64cd\u4f5cPPT\u6587\u4ef6\u3002python-pptx\u5e93\u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u7684\u5e93\uff0c\u5b83\u5141\u8bb8\u7528\u6237\u521b\u5efa\u3001\u4fee\u6539\u548c\u89e3\u6790PPT\u6587\u4ef6\u3002\u8981\u66f4\u65b0PPT\u4e2d\u7684\u6587\u672c\uff0c\u9996\u5148\u9700\u8981\u5b89\u88c5\u5e76\u5bfc\u5165\u8be5\u5e93\uff0c\u7136\u540e\u901a\u8fc7\u52a0\u8f7dPPT\u6587\u4ef6\uff0c\u5b9a\u4f4d\u5230\u9700\u8981\u4fee\u6539\u7684\u6587\u672c\u6846\uff0c\u6700\u540e\u8fdb\u884c\u6587\u672c\u66ff\u6362\u5e76\u4fdd\u5b58\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><p>\u5b89\u88c5python-pptx\u5e93\u53ef\u4ee5\u901a\u8fc7pip\u547d\u4ee4\u5b8c\u6210\uff0c\u4f7f\u7528\u547d\u4ee4\u201cpip install python-pptx\u201d\u5373\u53ef\u5b89\u88c5\u3002\u5728\u5b89\u88c5\u5b8c\u8be5\u5e93\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528Presentation\u7c7b\u52a0\u8f7dPPT\u6587\u4ef6\uff0c\u901a\u8fc7slides\u5c5e\u6027\u8bbf\u95ee\u5e7b\u706f\u7247\u5217\u8868\uff0c\u5e76\u901a\u8fc7shapes\u5c5e\u6027\u8bbf\u95ee\u6bcf\u5f20\u5e7b\u706f\u7247\u4e0a\u7684\u5f62\u72b6\uff0c\u5305\u62ec\u6587\u672c\u6846\u3002\u5728\u5b9a\u4f4d\u5230\u9700\u8981\u4fee\u6539\u7684\u6587\u672c\u6846\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7text\u5c5e\u6027\u83b7\u53d6\u548c\u4fee\u6539\u6587\u672c\u5185\u5bb9\u3002\u4fee\u6539\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528save\u65b9\u6cd5\u4fdd\u5b58\u66f4\u65b0\u540e\u7684PPT\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u5b89\u88c5\u548c\u5bfc\u5165python-pptx\u5e93<\/p>\n<\/p>\n<p><p>\u5728\u5f00\u59cb\u4f7f\u7528Python\u66f4\u65b0PPT\u6587\u672c\u4e4b\u524d\uff0c\u9700\u8981\u5b89\u88c5python-pptx\u5e93\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u8fdb\u884c\u5b89\u88c5\u548c\u5bfc\u5165\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p>\u4f7f\u7528pip\u547d\u4ee4\u5b89\u88c5python-pptx\u5e93\u3002\u6253\u5f00\u547d\u4ee4\u884c\u7ec8\u7aef\uff0c\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install python-pptx<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p>\u5728Python\u811a\u672c\u4e2d\u5bfc\u5165pptx\u5e93\u3002\u53ef\u4ee5\u5728\u4ee3\u7801\u7684\u5f00\u5934\u90e8\u5206\u6dfb\u52a0\u4ee5\u4e0b\u5bfc\u5165\u8bed\u53e5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pptx import Presentation<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u53ef\u4ee5\u6210\u529f\u5b89\u88c5\u548c\u5bfc\u5165python-pptx\u5e93\uff0c\u4e3a\u540e\u7eed\u64cd\u4f5cPPT\u6587\u4ef6\u505a\u597d\u51c6\u5907\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u52a0\u8f7dPPT\u6587\u4ef6\u5e76\u8bbf\u95ee\u5e7b\u706f\u7247<\/p>\n<\/p>\n<p><p>\u5b89\u88c5\u5e76\u5bfc\u5165python-pptx\u5e93\u540e\uff0c\u63a5\u4e0b\u6765\u9700\u8981\u52a0\u8f7d\u9700\u8981\u66f4\u65b0\u7684PPT\u6587\u4ef6\uff0c\u5e76\u8bbf\u95ee\u5176\u4e2d\u7684\u5e7b\u706f\u7247\u3002\u53ef\u4ee5\u6309\u7167\u4ee5\u4e0b\u6b65\u9aa4\u8fdb\u884c\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p>\u4f7f\u7528Presentation\u7c7b\u52a0\u8f7dPPT\u6587\u4ef6\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u4ee3\u7801\u52a0\u8f7d\u4e00\u4e2aPPT\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">prs = Presentation(&#39;path_to_ppt_file.pptx&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5176\u4e2d\uff0c&#39;path_to_ppt_file.pptx&#39;\u4e3aPPT\u6587\u4ef6\u7684\u8def\u5f84\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p>\u901a\u8fc7slides\u5c5e\u6027\u8bbf\u95ee\u5e7b\u706f\u7247\u5217\u8868\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u8bbf\u95eePPT\u6587\u4ef6\u4e2d\u7684\u6240\u6709\u5e7b\u706f\u7247\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">slides = prs.slides<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6b64\u65f6\uff0cslides\u662f\u4e00\u4e2a\u5217\u8868\uff0c\u5305\u542b\u4e86PPT\u6587\u4ef6\u4e2d\u7684\u6240\u6709\u5e7b\u706f\u7247\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u56db\u3001\u5b9a\u4f4d\u9700\u8981\u4fee\u6539\u7684\u6587\u672c\u6846<\/p>\n<\/p>\n<p><p>\u5728\u8bbf\u95ee\u5e7b\u706f\u7247\u540e\uff0c\u63a5\u4e0b\u6765\u9700\u8981\u5b9a\u4f4d\u5230\u9700\u8981\u4fee\u6539\u7684\u6587\u672c\u6846\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u8fdb\u884c\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p>\u901a\u8fc7\u5e7b\u706f\u7247\u5bf9\u8c61\u8bbf\u95ee\u5f62\u72b6\u3002\u6bcf\u4e2a\u5e7b\u706f\u7247\u5bf9\u8c61\u90fd\u6709\u4e00\u4e2ashapes\u5c5e\u6027\uff0c\u5305\u542b\u4e86\u5e7b\u706f\u7247\u4e0a\u7684\u6240\u6709\u5f62\u72b6\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u8bbf\u95ee\u5e7b\u706f\u7247\u4e0a\u7684\u5f62\u72b6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for slide in slides:<\/p>\n<p>    for shape in slide.shapes:<\/p>\n<p>        if not shape.has_text_frame:<\/p>\n<p>            continue<\/p>\n<p>        # \u5904\u7406\u6587\u672c\u6846<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p>\u5224\u65ad\u5f62\u72b6\u662f\u5426\u4e3a\u6587\u672c\u6846\u3002\u53ef\u4ee5\u901a\u8fc7has_text_frame\u5c5e\u6027\u5224\u65ad\u5f62\u72b6\u662f\u5426\u4e3a\u6587\u672c\u6846\u3002\u5982\u679c\u5f62\u72b6\u662f\u6587\u672c\u6846\uff0c\u5219\u53ef\u4ee5\u8fdb\u4e00\u6b65\u83b7\u53d6\u548c\u4fee\u6539\u6587\u672c\u5185\u5bb9\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p>\u5b9a\u4f4d\u5230\u9700\u8981\u4fee\u6539\u7684\u6587\u672c\u6846\u3002\u53ef\u4ee5\u6839\u636e\u6587\u672c\u6846\u4e2d\u7684\u6587\u672c\u5185\u5bb9\uff0c\u6216\u8005\u5176\u4ed6\u6761\u4ef6\uff08\u5982\u6587\u672c\u6846\u7684\u540d\u79f0\u3001\u4f4d\u7f6e\u7b49\uff09\u5b9a\u4f4d\u5230\u9700\u8981\u4fee\u6539\u7684\u6587\u672c\u6846\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e94\u3001\u8fdb\u884c\u6587\u672c\u66ff\u6362<\/p>\n<\/p>\n<p><p>\u5728\u5b9a\u4f4d\u5230\u9700\u8981\u4fee\u6539\u7684\u6587\u672c\u6846\u540e\uff0c\u53ef\u4ee5\u8fdb\u884c\u6587\u672c\u66ff\u6362\u3002\u4ee5\u4e0b\u662f\u8fdb\u884c\u6587\u672c\u66ff\u6362\u7684\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p>\u83b7\u53d6\u6587\u672c\u6846\u4e2d\u7684\u6587\u672c\u5185\u5bb9\u3002\u53ef\u4ee5\u901a\u8fc7text\u5c5e\u6027\u83b7\u53d6\u6587\u672c\u6846\u4e2d\u7684\u6587\u672c\u5185\u5bb9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = shape.text<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p>\u66ff\u6362\u6587\u672c\u5185\u5bb9\u3002\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u7684replace\u65b9\u6cd5\u8fdb\u884c\u6587\u672c\u66ff\u6362\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">new_text = text.replace(&#39;old_text&#39;, &#39;new_text&#39;)<\/p>\n<p>shape.text = new_text<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5176\u4e2d\uff0c&#39;old_text&#39;\u4e3a\u9700\u8981\u66ff\u6362\u7684\u65e7\u6587\u672c\uff0c&#39;new_text&#39;\u4e3a\u65b0\u7684\u6587\u672c\u5185\u5bb9\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u516d\u3001\u4fdd\u5b58\u66f4\u65b0\u540e\u7684PPT\u6587\u4ef6<\/p>\n<\/p>\n<p><p>\u5b8c\u6210\u6587\u672c\u66ff\u6362\u540e\uff0c\u9700\u8981\u5c06\u66f4\u65b0\u540e\u7684PPT\u6587\u4ef6\u4fdd\u5b58\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u8fdb\u884c\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p>\u4f7f\u7528save\u65b9\u6cd5\u4fdd\u5b58PPT\u6587\u4ef6\u3002\u53ef\u4ee5\u6307\u5b9a\u65b0\u7684\u6587\u4ef6\u540d\u4fdd\u5b58\u66f4\u65b0\u540e\u7684PPT\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">prs.save(&#39;updated_ppt_file.pptx&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5176\u4e2d\uff0c&#39;updated_ppt_file.pptx&#39;\u4e3a\u4fdd\u5b58\u540e\u7684PPT\u6587\u4ef6\u540d\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4ee5\u4e0a\u5c31\u662f\u4f7f\u7528Python\u66f4\u65b0PPT\u6587\u672c\u7684\u8be6\u7ec6\u6b65\u9aa4\u3002\u901a\u8fc7python-pptx\u5e93\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u52a0\u8f7dPPT\u6587\u4ef6\u3001\u5b9a\u4f4d\u9700\u8981\u4fee\u6539\u7684\u6587\u672c\u6846\u3001\u8fdb\u884c\u6587\u672c\u66ff\u6362\u5e76\u4fdd\u5b58\u66f4\u65b0\u540e\u7684\u6587\u4ef6\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u66f4\u65b0PPT\u4e2d\u7684\u6587\u672c\uff1f<\/strong><br \/>\u4f7f\u7528Python\u66f4\u65b0PPT\u4e2d\u7684\u6587\u672c\u901a\u5e38\u53ef\u4ee5\u901a\u8fc7<code>python-pptx<\/code>\u5e93\u6765\u5b9e\u73b0\u3002\u8fd9\u4e2a\u5e93\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u529f\u80fd\u6765\u64cd\u4f5cPowerPoint\u6587\u4ef6\uff0c\u5305\u62ec\u6587\u672c\u7684\u4fee\u6539\u3002\u9996\u5148\uff0c\u60a8\u9700\u8981\u5b89\u88c5\u8be5\u5e93\uff0c\u53ef\u4ee5\u901a\u8fc7\u8fd0\u884c<code>pip install python-pptx<\/code>\u8fdb\u884c\u5b89\u88c5\u3002\u4e4b\u540e\uff0c\u60a8\u53ef\u4ee5\u52a0\u8f7dPPT\u6587\u4ef6\uff0c\u904d\u5386\u5e7b\u706f\u7247\u548c\u5f62\u72b6\uff0c\u627e\u5230\u9700\u8981\u66f4\u65b0\u7684\u6587\u672c\u6846\u5e76\u8fdb\u884c\u4fee\u6539\u3002<\/p>\n<p><strong>\u80fd\u5426\u5728Python\u4e2d\u6279\u91cf\u66f4\u65b0\u591a\u4e2aPPT\u6587\u4ef6\u7684\u6587\u672c\uff1f<\/strong><br \/>\u662f\u7684\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528Python\u811a\u672c\u6279\u91cf\u5904\u7406\u591a\u4e2aPPT\u6587\u4ef6\u3002\u901a\u8fc7\u7f16\u5199\u4e00\u4e2a\u5faa\u73af\uff0c\u60a8\u53ef\u4ee5\u9010\u4e00\u52a0\u8f7d\u6bcf\u4e2aPPT\u6587\u4ef6\uff0c\u67e5\u627e\u5e76\u66f4\u65b0\u6587\u672c\u5185\u5bb9\u3002\u8fd9\u5728\u9700\u8981\u5bf9\u591a\u4e2a\u6f14\u793a\u6587\u7a3f\u8fdb\u884c\u76f8\u540c\u66f4\u6539\u65f6\u975e\u5e38\u9ad8\u6548\u3002\u786e\u4fdd\u5728\u5904\u7406\u6587\u4ef6\u65f6\uff0c\u60a8\u6709\u9002\u5f53\u7684\u5907\u4efd\uff0c\u4ee5\u9632\u51fa\u73b0\u610f\u5916\u60c5\u51b5\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u66f4\u65b0PPT\u6587\u672c\u7684\u683c\u5f0f\uff0c\u4f8b\u5982\u5b57\u4f53\u548c\u989c\u8272\uff1f<\/strong><br \/>\u5f53\u7136\u53ef\u4ee5\uff01<code>python-pptx<\/code>\u5e93\u4e0d\u4ec5\u5141\u8bb8\u60a8\u66f4\u65b0\u6587\u672c\u5185\u5bb9\uff0c\u8fd8\u652f\u6301\u66f4\u6539\u6587\u672c\u7684\u683c\u5f0f\u3002\u60a8\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u5b57\u4f53\u5927\u5c0f\u3001\u5b57\u4f53\u989c\u8272\u548c\u6837\u5f0f\u7b49\u5c5e\u6027\u6765\u7f8e\u5316\u6587\u672c\u3002\u5177\u4f53\u64cd\u4f5c\u53ef\u4ee5\u901a\u8fc7\u8bbf\u95ee\u5f62\u72b6\u7684<code>text_frame<\/code>\u5c5e\u6027\uff0c\u5e76\u5bf9<code>Paragraph<\/code>\u548c<code>Run<\/code>\u5bf9\u8c61\u8fdb\u884c\u76f8\u5e94\u7684\u5c5e\u6027\u8bbe\u7f6e\u6765\u5b9e\u73b0\u3002\u8fd9\u6837\uff0c\u60a8\u5c31\u80fd\u6839\u636e\u9700\u6c42\u8c03\u6574\u6587\u672c\u7684\u5916\u89c2\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4e00\u3001\u4f7f\u7528Python\u66f4\u65b0PPT\u6587\u672c\u7684\u65b9\u6cd5 \u4f7f\u7528Python\u66f4\u65b0PPT\u6587\u672c\u53ef\u4ee5\u901a\u8fc7python-pptx\u5e93\u3001\u5b9a\u4f4d [&hellip;]","protected":false},"author":3,"featured_media":974939,"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\/974930"}],"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=974930"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/974930\/revisions"}],"predecessor-version":[{"id":974942,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/974930\/revisions\/974942"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/974939"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=974930"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=974930"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=974930"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}