{"id":1262,"date":"2022-12-09T18:13:13","date_gmt":"2022-12-09T10:13:13","guid":{"rendered":"https:\/\/www.iotot.com\/?p=1262"},"modified":"2022-12-09T18:13:13","modified_gmt":"2022-12-09T10:13:13","slug":"python%e5%8f%af%e4%bb%a5%e6%93%8d%e4%bd%9cword%e6%96%87%e4%bb%b6%e5%90%97%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/blog.iotot.com\/?p=1262","title":{"rendered":"python\u53ef\u4ee5\u64cd\u4f5cword\u6587\u4ef6\u5417\uff1f"},"content":{"rendered":"<p>Python\u53ef\u4ee5\u64cd\u4f5cWord\u6587\u4ef6\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528Python\u7684\u7b2c\u4e09\u65b9\u5e93\uff0c\u5982python-docx\u3001pywin32\u7b49\uff0c\u6765\u5b9e\u73b0\u5bf9Word\u6587\u4ef6\u7684\u64cd\u4f5c\u3002\u901a\u8fc7\u8fd9\u4e9b\u5e93\uff0c\u4f60\u53ef\u4ee5\u5b9e\u73b0\u5bf9Word\u6587\u4ef6\u7684\u8bfb\u53d6\u3001\u5199\u5165\u3001\u4fee\u6539\u3001\u683c\u5f0f\u5316\u7b49\u64cd\u4f5c\u3002<\/p>\n<p>\u4f8b\u5982\uff0c\u4f7f\u7528python-docx\u5e93\uff0c\u4f60\u53ef\u4ee5\u8fd9\u6837\u64cd\u4f5cWord\u6587\u4ef6\uff1a<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\"># \u5bfc\u5165python-docx\u5e93\r\nimport docx\r\n\r\n# \u8bfb\u53d6Word\u6587\u4ef6\r\ndocument = docx.Document('\u6587\u4ef6\u540d.docx')\r\n\r\n# \u83b7\u53d6\u6587\u4ef6\u4e2d\u7684\u6240\u6709\u6bb5\u843d\r\nparagraphs = document.paragraphs\r\n\r\n# \u904d\u5386\u6bb5\u843d\uff0c\u5e76\u6253\u5370\u51fa\u6765\r\nfor paragraph in paragraphs:\r\n    print(paragraph.text)\r\n\r\n# \u5728\u672b\u5c3e\u6dfb\u52a0\u4e00\u4e2a\u6bb5\u843d\r\ndocument.add_paragraph('\u8fd9\u662f\u4e00\u4e2a\u65b0\u6bb5\u843d')\r\n\r\n# \u4fdd\u5b58\u4fee\u6539\u540e\u7684Word\u6587\u4ef6\r\ndocument.save('\u6587\u4ef6\u540d.docx')\r\n<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python\u53ef\u4ee5\u64cd\u4f5cWord\u6587\u4ef6\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528Python\u7684\u7b2c\u4e09\u65b9\u5e93\uff0c\u5982python-docx\u3001pywin32\u7b49 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[95],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.iotot.com\/index.php?rest_route=\/wp\/v2\/posts\/1262"}],"collection":[{"href":"https:\/\/blog.iotot.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.iotot.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.iotot.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.iotot.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1262"}],"version-history":[{"count":1,"href":"https:\/\/blog.iotot.com\/index.php?rest_route=\/wp\/v2\/posts\/1262\/revisions"}],"predecessor-version":[{"id":1263,"href":"https:\/\/blog.iotot.com\/index.php?rest_route=\/wp\/v2\/posts\/1262\/revisions\/1263"}],"wp:attachment":[{"href":"https:\/\/blog.iotot.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1262"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.iotot.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1262"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.iotot.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1262"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}