{"id":971290,"date":"2024-12-27T05:33:38","date_gmt":"2024-12-26T21:33:38","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/971290.html"},"modified":"2024-12-27T05:33:39","modified_gmt":"2024-12-26T21:33:39","slug":"python%e5%a6%82%e4%bd%95%e8%af%bb%e5%8f%96ppt%e6%96%87%e6%a1%a3","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/971290.html","title":{"rendered":"python\u5982\u4f55\u8bfb\u53d6ppt\u6587\u6863"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24194206\/0b9fb431-e38e-4294-bc6c-4c2c5e73698b.webp\" alt=\"python\u5982\u4f55\u8bfb\u53d6ppt\u6587\u6863\" \/><\/p>\n<p><p> <strong>Python\u8bfb\u53d6PPT\u6587\u6863\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u51e0\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff1a\u4f7f\u7528python-pptx\u5e93\u3001\u4f7f\u7528comtypes\u5e93\u3001\u4f7f\u7528mammoth\u548cpython-docx\u5e93\u3002<\/strong>\u5176\u4e2d\uff0c\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u4f7f\u7528python-pptx\u5e93\uff0c\u56e0\u4e3a\u5b83\u4e13\u95e8\u7528\u4e8e\u5904\u7406PPT\u6587\u4ef6\uff0c\u529f\u80fd\u5f3a\u5927\u4e14\u6613\u4e8e\u4f7f\u7528\u3002\u4e0b\u9762\uff0c\u6211\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528python-pptx\u5e93\u6765\u8bfb\u53d6PPT\u6587\u6863\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b89\u88c5\u548c\u5bfc\u5165\u5e93<\/h3>\n<\/p>\n<p><p>\u8981\u4f7f\u7528python-pptx\u5e93\uff0c\u9996\u5148\u9700\u8981\u5b89\u88c5\u5b83\u3002\u53ef\u4ee5\u4f7f\u7528pip\u547d\u4ee4\u8fdb\u884c\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>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u5728Python\u811a\u672c\u4e2d\u5bfc\u5165\u8be5\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pptx import Presentation<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528python-pptx\u5e93\u8bfb\u53d6PPT\u6587\u6863<\/h3>\n<\/p>\n<ol>\n<li>\n<p><strong>\u52a0\u8f7dPPT\u6587\u4ef6<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528<code>Presentation<\/code>\u7c7b\u52a0\u8f7dPPT\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">prs = Presentation(&#39;example.pptx&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u91cc\uff0c<code>example.pptx<\/code>\u662f\u8981\u8bfb\u53d6\u7684PPT\u6587\u4ef6\u7684\u8def\u5f84\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8bfb\u53d6\u5e7b\u706f\u7247<\/strong><\/p>\n<\/p>\n<p><p>\u52a0\u8f7dPPT\u6587\u4ef6\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u904d\u5386<code>slides<\/code>\u5c5e\u6027\u6765\u8bfb\u53d6\u6bcf\u4e00\u5f20\u5e7b\u706f\u7247\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for slide in prs.slides:<\/p>\n<p>    # \u8bfb\u53d6\u6bcf\u5f20\u5e7b\u706f\u7247\u7684\u5185\u5bb9<\/p>\n<p>    pass<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8bfb\u53d6\u6587\u672c\u5185\u5bb9<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u6bcf\u5f20\u5e7b\u706f\u7247\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u904d\u5386<code>shapes<\/code>\u5c5e\u6027\u6765\u8bfb\u53d6\u6587\u672c\u6846\u7684\u5185\u5bb9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for slide in prs.slides:<\/p>\n<p>    for shape in slide.shapes:<\/p>\n<p>        if hasattr(shape, &quot;text&quot;):<\/p>\n<p>            print(shape.text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u91cc\u4f7f\u7528<code>hasattr<\/code>\u51fd\u6570\u6765\u68c0\u67e5\u5f62\u72b6\u5bf9\u8c61\u662f\u5426\u5177\u6709<code>text<\/code>\u5c5e\u6027\uff0c\u4ee5\u786e\u4fdd\u53ea\u8bfb\u53d6\u6587\u672c\u6846\u7684\u5185\u5bb9\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u4e09\u3001\u8bfb\u53d6\u66f4\u590d\u6742\u7684\u5185\u5bb9<\/h3>\n<\/p>\n<ol>\n<li>\n<p><strong>\u8bfb\u53d6\u8868\u683c<\/strong><\/p>\n<\/p>\n<p><p>\u5982\u679c\u5e7b\u706f\u7247\u4e2d\u5305\u542b\u8868\u683c\uff0c\u53ef\u4ee5\u901a\u8fc7\u68c0\u67e5\u5f62\u72b6\u7c7b\u578b\u6765\u8bfb\u53d6\u8868\u683c\u5185\u5bb9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pptx.enum.shapes import MSO_SHAPE<\/p>\n<p>for slide in prs.slides:<\/p>\n<p>    for shape in slide.shapes:<\/p>\n<p>        if shape.has_table:<\/p>\n<p>            table = shape.table<\/p>\n<p>            for row in table.rows:<\/p>\n<p>                for cell in row.cells:<\/p>\n<p>                    print(cell.text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7<code>has_table<\/code>\u5c5e\u6027\u68c0\u67e5\u5f62\u72b6\u662f\u5426\u4e3a\u8868\u683c\uff0c\u5e76\u904d\u5386\u884c\u548c\u5355\u5143\u683c\u8bfb\u53d6\u5185\u5bb9\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8bfb\u53d6\u56fe\u7247\u548c\u56fe\u8868<\/strong><\/p>\n<\/p>\n<p><p>\u5bf9\u4e8e\u5305\u542b\u56fe\u7247\u6216\u56fe\u8868\u7684\u5e7b\u706f\u7247\uff0c\u53ef\u4ee5\u4f7f\u7528\u7c7b\u4f3c\u7684\u65b9\u6cd5\u8bfb\u53d6\u5f62\u72b6\u7c7b\u578b\uff0c\u5e76\u5904\u7406\u76f8\u5e94\u5185\u5bb9\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u56db\u3001\u4f7f\u7528comtypes\u5e93\u8bfb\u53d6PPT\u6587\u6863<\/h3>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b89\u88c5comtypes\u5e93<\/strong><\/p>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528pip\u547d\u4ee4\u5b89\u88c5comtypes\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install comtypes<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8bfb\u53d6PPT\u6587\u4ef6<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528comtypes\u5e93\u53ef\u4ee5\u901a\u8fc7\u64cd\u4f5cPowerPoint\u5e94\u7528\u7a0b\u5e8f\u6765\u8bfb\u53d6PPT\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import comtypes.client<\/p>\n<p>powerpoint = comtypes.client.CreateObject(&quot;PowerPoint.Application&quot;)<\/p>\n<p>presentation = powerpoint.Presentations.Open(&#39;example.pptx&#39;)<\/p>\n<p>for slide in presentation.Slides:<\/p>\n<p>    for shape in slide.Shapes:<\/p>\n<p>        if shape.HasTextFrame:<\/p>\n<p>            print(shape.TextFrame.TextRange.Text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528comtypes\u5e93\u9700\u8981\u786e\u4fdd\u5b89\u88c5\u4e86Microsoft Office\u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u4e94\u3001\u4f7f\u7528mammoth\u548cpython-docx\u5e93\u8bfb\u53d6PPT\u6587\u6863<\/h3>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b89\u88c5mammoth\u548cpython-docx\u5e93<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install mammoth python-docx<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8bfb\u53d6PPT\u6587\u4ef6<\/strong><\/p>\n<\/p>\n<p><p>\u867d\u7136mammoth\u548cpython-docx\u5e93\u4e3b\u8981\u7528\u4e8e\u5904\u7406Word\u6587\u6863\uff0c\u4f46\u53ef\u4ee5\u7ed3\u5408\u4f7f\u7528\u6765\u8bfb\u53d6PPT\u6587\u4ef6\u7684\u6587\u672c\u5185\u5bb9\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u51e0\u79cd\u65b9\u6cd5\uff0cPython\u53ef\u4ee5\u6709\u6548\u5730\u8bfb\u53d6PPT\u6587\u6863\u7684\u5185\u5bb9\u3002<strong>python-pptx\u5e93<\/strong>\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\uff0c\u56e0\u4e3a\u5b83\u4e13\u95e8\u7528\u4e8e\u5904\u7406PPT\u6587\u4ef6\uff0c\u529f\u80fd\u5f3a\u5927\u4e14\u6613\u4e8e\u4f7f\u7528\uff1b<strong>comtypes\u5e93<\/strong>\u9002\u7528\u4e8e\u9700\u8981\u76f4\u63a5\u64cd\u4f5cPowerPoint\u5e94\u7528\u7a0b\u5e8f\u7684\u573a\u666f\uff1b<strong>mammoth\u548cpython-docx\u5e93<\/strong>\u53ef\u4ee5\u7ed3\u5408\u4f7f\u7528\u6765\u8bfb\u53d6PPT\u6587\u4ef6\u7684\u6587\u672c\u5185\u5bb9\u3002\u9009\u62e9\u9002\u5408\u7684\u65b9\u6cd5\u53ef\u4ee5\u63d0\u9ad8\u5de5\u4f5c\u6548\u7387\uff0c\u6ee1\u8db3\u4e0d\u540c\u7684\u9700\u6c42\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u8bfb\u53d6PPT\u6587\u6863\u4e2d\u7684\u6587\u672c\u5185\u5bb9\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528<code>python-pptx<\/code>\u5e93\u6765\u8bfb\u53d6PPT\u6587\u6863\u4e2d\u7684\u6587\u672c\u5185\u5bb9\u3002\u9996\u5148\uff0c\u9700\u8981\u5b89\u88c5\u8be5\u5e93\uff0c\u65b9\u6cd5\u662f\u4f7f\u7528\u547d\u4ee4<code>pip install python-pptx<\/code>\u3002\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u901a\u8fc7\u6253\u5f00PPT\u6587\u4ef6\u5e76\u904d\u5386\u6bcf\u4e00\u9875\u4e2d\u7684\u5f62\u72b6\uff0c\u53ef\u4ee5\u63d0\u53d6\u6587\u672c\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">from pptx import Presentation\n\nppt = Presentation(&#39;your_presentation.pptx&#39;)\nfor slide in ppt.slides:\n    for shape in slide.shapes:\n        if hasattr(shape, &quot;text&quot;):\n            print(shape.text)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u8f7b\u677e\u83b7\u53d6\u5e7b\u706f\u7247\u4e2d\u7684\u6240\u6709\u6587\u672c\u4fe1\u606f\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u4f7f\u7528Python\u8bfb\u53d6PPT\u4e2d\u7684\u56fe\u7247\u6216\u56fe\u8868\uff1f<\/strong><br \/>\u5f53\u7136\u53ef\u4ee5\u3002<code>python-pptx<\/code>\u5e93\u4e0d\u4ec5\u652f\u6301\u8bfb\u53d6\u6587\u672c\uff0c\u8fd8\u80fd\u63d0\u53d6\u5e7b\u706f\u7247\u4e2d\u7684\u56fe\u7247\u548c\u56fe\u8868\u3002\u901a\u8fc7\u904d\u5386\u5f62\u72b6\u5bf9\u8c61\uff0c\u53ef\u4ee5\u68c0\u67e5\u5176\u7c7b\u578b\uff0c\u5982\u679c\u662f\u56fe\u7247\uff0c\u5219\u53ef\u4ee5\u83b7\u53d6\u56fe\u7247\u7684\u76f8\u5173\u4fe1\u606f\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">for slide in ppt.slides:\n    for shape in slide.shapes:\n        if shape.shape_type == 13:  # 13\u4ee3\u8868\u56fe\u7247\n            image = shape.image\n            print(image.filename)  # \u6253\u5370\u56fe\u7247\u6587\u4ef6\u540d\n<\/code><\/pre>\n<p>\u8fd9\u6837\u80fd\u591f\u5e2e\u52a9\u7528\u6237\u83b7\u53d6PPT\u4e2d\u7684\u591a\u5a92\u4f53\u5185\u5bb9\u3002<\/p>\n<p><strong>\u4f7f\u7528Python\u8bfb\u53d6PPT\u6587\u4ef6\u65f6\uff0c\u9047\u5230\u683c\u5f0f\u517c\u5bb9\u6027\u95ee\u9898\u8be5\u5982\u4f55\u89e3\u51b3\uff1f<\/strong><br \/>\u5728\u5904\u7406PPT\u6587\u6863\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u56e0\u4e0d\u540c\u7248\u672c\u6216\u683c\u5f0f\u5bfc\u81f4\u7684\u517c\u5bb9\u6027\u95ee\u9898\u3002\u4e3a\u4e86\u89e3\u51b3\u6b64\u7c7b\u95ee\u9898\uff0c\u53ef\u4ee5\u5c1d\u8bd5\u5c06PPT\u6587\u4ef6\u53e6\u5b58\u4e3a\u4e0d\u540c\u683c\u5f0f\uff08\u4f8b\u5982\uff0c.pptx\u683c\u5f0f\uff09\u518d\u8fdb\u884c\u5904\u7406\u3002\u6b64\u5916\uff0c\u786e\u4fdd\u4f7f\u7528\u6700\u65b0\u7248\u672c\u7684<code>python-pptx<\/code>\u5e93\uff0c\u56e0\u4e3a\u5b83\u4f1a\u4e0d\u65ad\u66f4\u65b0\u4ee5\u652f\u6301\u66f4\u591a\u7684\u529f\u80fd\u548c\u683c\u5f0f\u3002\u5982\u679c\u4ecd\u7136\u6709\u95ee\u9898\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u5176\u4ed6\u5e93\uff0c\u5982<code>PyWin32<\/code>\uff0c\u5728Windows\u73af\u5883\u4e0b\u76f4\u63a5\u5229\u7528Microsoft PowerPoint\u7684COM\u63a5\u53e3\u6765\u5904\u7406PPT\u6587\u4ef6\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u8bfb\u53d6PPT\u6587\u6863\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u51e0\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff1a\u4f7f\u7528python-pptx\u5e93\u3001\u4f7f\u7528comtypes\u5e93\u3001\u4f7f\u7528 [&hellip;]","protected":false},"author":3,"featured_media":971294,"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\/971290"}],"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=971290"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/971290\/revisions"}],"predecessor-version":[{"id":971296,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/971290\/revisions\/971296"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/971294"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=971290"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=971290"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=971290"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}