{"id":1108400,"date":"2025-01-08T17:00:00","date_gmt":"2025-01-08T09:00:00","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1108400.html"},"modified":"2025-01-08T17:00:06","modified_gmt":"2025-01-08T09:00:06","slug":"python%e5%a6%82%e4%bd%95%e8%af%bb%e5%8f%96doc%e6%96%87%e4%bb%b6%e8%a6%81%e5%ae%89%e8%a3%85%e7%9a%84%e5%8c%85","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1108400.html","title":{"rendered":"python\u5982\u4f55\u8bfb\u53d6doc\u6587\u4ef6\u8981\u5b89\u88c5\u7684\u5305"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25072055\/666b93a8-72f3-41c3-aef4-1b10a6a7248c.webp\" alt=\"python\u5982\u4f55\u8bfb\u53d6doc\u6587\u4ef6\u8981\u5b89\u88c5\u7684\u5305\" \/><\/p>\n<p><p> <strong>Python \u8bfb\u53d6 doc \u6587\u4ef6\u9700\u8981\u5b89\u88c5\u7684\u5305\u6709\uff1apython-docx\u3001pywin32\u3001docx2txt<\/strong>\u3002\u5176\u4e2d\uff0c\u6700\u5e38\u7528\u7684\u5305\u662f <strong>python-docx<\/strong>\uff0c\u56e0\u4e3a\u5b83\u80fd\u591f\u65b9\u4fbf\u5730\u5904\u7406 Word \u6587\u4ef6\u7684\u8bfb\u5199\u64cd\u4f5c\u3002\u4ee5\u4e0b\u662f\u8be6\u7ec6\u63cf\u8ff0\u548c\u4f7f\u7528\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b89\u88c5 python-docx \u5305<\/h3>\n<\/p>\n<p><p><strong>python-docx \u662f\u4e00\u4e2a\u7528\u4e8e\u521b\u5efa\u548c\u66f4\u65b0 Microsoft Word\uff08.docx\uff09\u6587\u4ef6\u7684 Python \u5e93\u3002<\/strong> \u4f7f\u7528 pip \u5b89\u88c5 python-docx \u5305\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u5728\u7ec8\u7aef\u6216\u547d\u4ee4\u63d0\u793a\u7b26\u4e2d\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install python-docx<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u8bfb\u53d6 docx \u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528 python-docx \u5305\u6765\u8bfb\u53d6 docx \u6587\u4ef6\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from docx import Document<\/p>\n<h2><strong>\u52a0\u8f7d\u6587\u6863<\/strong><\/h2>\n<p>doc = Document(&#39;your-file.docx&#39;)<\/p>\n<h2><strong>\u8bfb\u53d6\u6bcf\u4e00\u6bb5\u7684\u5185\u5bb9<\/strong><\/h2>\n<p>for para in doc.paragraphs:<\/p>\n<p>    print(para.text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5bfc\u5165\u4e86 Document \u7c7b\uff0c\u7136\u540e\u4f7f\u7528 Document \u7c7b\u52a0\u8f7d\u8981\u8bfb\u53d6\u7684 Word \u6587\u6863\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u904d\u5386\u6587\u6863\u4e2d\u7684\u6bcf\u4e00\u6bb5\uff0c\u5e76\u8f93\u51fa\u6bb5\u843d\u7684\u6587\u672c\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/strong><\/p>\n<\/p>\n<p><p><strong>1. \u52a0\u8f7d\u6587\u6863<\/strong><\/p>\n<\/p>\n<p><p>\u8981\u8bfb\u53d6 docx \u6587\u4ef6\uff0c\u9996\u5148\u9700\u8981\u52a0\u8f7d\u6587\u6863\u3002\u53ef\u4ee5\u901a\u8fc7\u4f20\u9012\u6587\u4ef6\u8def\u5f84\u7ed9 Document \u7c7b\u7684\u6784\u9020\u51fd\u6570\u6765\u5b9e\u73b0\u3002\u8fd9\u5c06\u8fd4\u56de\u4e00\u4e2a Document \u5bf9\u8c61\uff0c\u8be5\u5bf9\u8c61\u8868\u793a\u52a0\u8f7d\u7684 Word \u6587\u6863\u3002<\/p>\n<\/p>\n<p><p><strong>2. \u904d\u5386\u6bb5\u843d<\/strong><\/p>\n<\/p>\n<p><p>\u52a0\u8f7d\u6587\u6863\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bbf\u95ee doc.paragraphs \u5c5e\u6027\u6765\u83b7\u53d6\u6587\u6863\u4e2d\u7684\u6240\u6709\u6bb5\u843d\u3002\u8be5\u5c5e\u6027\u8fd4\u56de\u4e00\u4e2a\u6bb5\u843d\u5bf9\u8c61\u5217\u8868\u3002\u53ef\u4ee5\u904d\u5386\u8fd9\u4e2a\u5217\u8868\uff0c\u5e76\u901a\u8fc7 para.text \u5c5e\u6027\u8bbf\u95ee\u6bcf\u4e2a\u6bb5\u843d\u7684\u6587\u672c\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u5b89\u88c5 pywin32 \u5305<\/h3>\n<\/p>\n<p><p><strong>pywin32 \u662f\u4e00\u4e2a\u5e93\uff0c\u5141\u8bb8\u5728 Windows \u4e0a\u4f7f\u7528 Python \u8fdb\u884c COM\uff08\u7ec4\u4ef6\u5bf9\u8c61\u6a21\u578b\uff09\u7f16\u7a0b\u3002<\/strong> \u4f7f\u7528 pip \u5b89\u88c5 pywin32 \u5305\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pywin32<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u8bfb\u53d6 doc \u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528 pywin32 \u5305\u53ef\u4ee5\u8bfb\u53d6 doc \u6587\u4ef6\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import win32com.client<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a Word \u5e94\u7528\u7a0b\u5e8f\u5bf9\u8c61<\/strong><\/h2>\n<p>word = win32com.client.Dispatch(&quot;Word.Application&quot;)<\/p>\n<h2><strong>\u6253\u5f00\u6587\u6863<\/strong><\/h2>\n<p>doc = word.Documents.Open(&#39;your-file.doc&#39;)<\/p>\n<h2><strong>\u8bfb\u53d6\u6bcf\u4e00\u6bb5\u7684\u5185\u5bb9<\/strong><\/h2>\n<p>for para in doc.Paragraphs:<\/p>\n<p>    print(para.Range.Text)<\/p>\n<h2><strong>\u5173\u95ed\u6587\u6863<\/strong><\/h2>\n<p>doc.Close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 win32com.client \u6a21\u5757\u521b\u5efa\u4e00\u4e2a Word \u5e94\u7528\u7a0b\u5e8f\u5bf9\u8c61\uff0c\u7136\u540e\u4f7f\u7528\u8be5\u5bf9\u8c61\u6253\u5f00\u8981\u8bfb\u53d6\u7684 doc \u6587\u4ef6\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u904d\u5386\u6587\u6863\u4e2d\u7684\u6bcf\u4e00\u6bb5\uff0c\u5e76\u8f93\u51fa\u6bb5\u843d\u7684\u6587\u672c\u5185\u5bb9\u3002\u6700\u540e\uff0c\u6211\u4eec\u5173\u95ed\u6587\u6863\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u5b89\u88c5 docx2txt \u5305<\/h3>\n<\/p>\n<p><p><strong>docx2txt \u662f\u4e00\u4e2a\u7528\u4e8e\u4ece docx \u6587\u4ef6\u4e2d\u63d0\u53d6\u6587\u672c\u7684 Python \u5e93\u3002<\/strong> \u4f7f\u7528 pip \u5b89\u88c5 docx2txt \u5305\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install docx2txt<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u8bfb\u53d6 docx \u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528 docx2txt \u5305\u6765\u8bfb\u53d6 docx \u6587\u4ef6\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import docx2txt<\/p>\n<h2><strong>\u52a0\u8f7d\u6587\u6863\u5e76\u63d0\u53d6\u6587\u672c<\/strong><\/h2>\n<p>text = docx2txt.process(&#39;your-file.docx&#39;)<\/p>\n<h2><strong>\u8f93\u51fa\u6587\u672c\u5185\u5bb9<\/strong><\/h2>\n<p>print(text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5bfc\u5165\u4e86 docx2txt \u6a21\u5757\uff0c\u7136\u540e\u4f7f\u7528 docx2txt.process \u51fd\u6570\u52a0\u8f7d\u8981\u8bfb\u53d6\u7684 Word \u6587\u6863\u5e76\u63d0\u53d6\u5176\u4e2d\u7684\u6587\u672c\u5185\u5bb9\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u8f93\u51fa\u63d0\u53d6\u7684\u6587\u672c\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/strong><\/p>\n<\/p>\n<p><p><strong>1. \u52a0\u8f7d\u6587\u6863\u5e76\u63d0\u53d6\u6587\u672c<\/strong><\/p>\n<\/p>\n<p><p>\u8981\u8bfb\u53d6 docx \u6587\u4ef6\u5e76\u63d0\u53d6\u6587\u672c\uff0c\u53ef\u4ee5\u4f7f\u7528 docx2txt.process \u51fd\u6570\u3002\u8be5\u51fd\u6570\u63a5\u53d7\u6587\u4ef6\u8def\u5f84\u4f5c\u4e3a\u53c2\u6570\uff0c\u5e76\u8fd4\u56de\u63d0\u53d6\u7684\u6587\u672c\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728 Python \u4e2d\u8bfb\u53d6 doc \u6587\u4ef6\u9700\u8981\u5b89\u88c5\u7684\u5305\u6709\uff1apython-docx\u3001pywin32\u3001docx2txt\u3002<strong>python-docx<\/strong> \u662f\u6700\u5e38\u7528\u7684\u5305\uff0c\u56e0\u4e3a\u5b83\u80fd\u591f\u65b9\u4fbf\u5730\u5904\u7406 Word \u6587\u4ef6\u7684\u8bfb\u5199\u64cd\u4f5c\u3002<strong>pywin32<\/strong> \u5305\u53ef\u4ee5\u5728 Windows \u5e73\u53f0\u4e0a\u4f7f\u7528\uff0c\u7528\u4e8e\u5904\u7406 doc \u6587\u4ef6\u3002<strong>docx2txt<\/strong> \u5305\u53ef\u4ee5\u4ece docx \u6587\u4ef6\u4e2d\u63d0\u53d6\u6587\u672c\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><p><strong>\u603b\u7684\u6765\u8bf4\uff0cpython-docx \u662f\u8bfb\u53d6\u548c\u5904\u7406 docx \u6587\u4ef6\u7684\u6700\u4f73\u9009\u62e9\uff0c\u800c pywin32 \u662f\u5904\u7406 doc \u6587\u4ef6\u7684\u4e00\u4e2a\u9009\u62e9\uff0cdocx2txt \u53ef\u4ee5\u7528\u4e8e\u5feb\u901f\u63d0\u53d6 docx \u6587\u4ef6\u4e2d\u7684\u6587\u672c\u5185\u5bb9\u3002<\/strong> \u9009\u62e9\u9002\u5408\u4f60\u7684\u9700\u6c42\u7684\u5305\uff0c\u5e76\u6839\u636e\u5177\u4f53\u60c5\u51b5\u4f7f\u7528\u76f8\u5e94\u7684\u4ee3\u7801\u8bfb\u53d6 Word \u6587\u4ef6\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8bfb\u53d6doc\u6587\u4ef6\uff1f\u9700\u8981\u54ea\u4e9b\u5e93\uff1f<\/strong><br \/>\u5728Python\u4e2d\u8bfb\u53d6doc\u6587\u4ef6\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>python-docx<\/code>\u5e93\u3002\u8fd9\u4e2a\u5e93\u5141\u8bb8\u60a8\u5904\u7406Word\u6587\u6863\uff0c\u5305\u62ec\u8bfb\u53d6\u6587\u672c\u3001\u6bb5\u843d\u548c\u5176\u4ed6\u5185\u5bb9\u3002\u5b89\u88c5\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u547d\u4ee4<code>pip install python-docx<\/code>\u6765\u83b7\u53d6\u8fd9\u4e2a\u5e93\u3002\u8bf7\u6ce8\u610f\uff0c<code>python-docx<\/code>\u4e3b\u8981\u7528\u4e8e\u5904\u7406.docx\u683c\u5f0f\u7684\u6587\u4ef6\uff0c\u5bf9\u4e8e.doc\u683c\u5f0f\u7684\u6587\u4ef6\uff0c\u60a8\u53ef\u80fd\u9700\u8981\u4f7f\u7528<code>pywin32<\/code>\u6216<code>pycom<\/code>\u5e93\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u8bfb\u53d6.doc\u683c\u5f0f\u7684\u6587\u4ef6\u800c\u4e0d\u5b89\u88c5\u989d\u5916\u7684\u5e93\uff1f<\/strong><br \/>\u76f4\u63a5\u5728Python\u4e2d\u8bfb\u53d6.doc\u683c\u5f0f\u7684\u6587\u4ef6\u800c\u4e0d\u4f7f\u7528\u4efb\u4f55\u5e93\u662f\u4e0d\u592a\u53ef\u80fd\u7684\uff0c\u56e0\u4e3aPython\u672c\u8eab\u4e0d\u652f\u6301\u8fd9\u79cd\u6587\u4ef6\u683c\u5f0f\u3002\u5982\u679c\u4e0d\u60f3\u5b89\u88c5\u989d\u5916\u7684\u5e93\uff0c\u60a8\u53ef\u4ee5\u8003\u8651\u5c06.doc\u6587\u4ef6\u8f6c\u6362\u4e3a.docx\u683c\u5f0f\uff0c\u7136\u540e\u4f7f\u7528<code>python-docx<\/code>\u5e93\u8fdb\u884c\u8bfb\u53d6\u3002\u8fd8\u6709\u4e00\u79cd\u65b9\u6cd5\u662f\u5c06\u6587\u4ef6\u8f6c\u6362\u4e3a\u6587\u672c\u683c\u5f0f\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u7528Python\u7684\u5185\u7f6e\u6587\u4ef6\u8bfb\u53d6\u529f\u80fd\u6765\u5904\u7406\u3002<\/p>\n<p><strong>\u8bfb\u53d6doc\u6587\u4ef6\u65f6\u9047\u5230\u4e71\u7801\uff0c\u8be5\u5982\u4f55\u89e3\u51b3\uff1f<\/strong><br \/>\u5982\u679c\u5728\u8bfb\u53d6doc\u6587\u4ef6\u65f6\u51fa\u73b0\u4e71\u7801\uff0c\u53ef\u80fd\u662f\u7531\u4e8e\u7f16\u7801\u95ee\u9898\u3002\u5efa\u8bae\u4f7f\u7528<code>python-docx<\/code>\u5e93\u5904\u7406.docx\u6587\u4ef6\uff0c\u56e0\u4e3a\u5b83\u80fd\u66f4\u597d\u5730\u7ba1\u7406\u6587\u672c\u7f16\u7801\u3002\u5982\u679c\u60a8\u4ecd\u9700\u8981\u5904\u7406.doc\u683c\u5f0f\uff0c\u5c1d\u8bd5\u4f7f\u7528<code>pywin32<\/code>\u5e93\uff0c\u5b83\u53ef\u4ee5\u901a\u8fc7\u8c03\u7528Windows\u7684Word\u5e94\u7528\u7a0b\u5e8f\u6765\u6253\u5f00\u6587\u4ef6\uff0c\u4ece\u800c\u907f\u514d\u4e71\u7801\u95ee\u9898\u3002\u5728\u5904\u7406\u6587\u672c\u65f6\uff0c\u786e\u4fdd\u4f7f\u7528\u6b63\u786e\u7684\u7f16\u7801\u683c\u5f0f\uff0c\u4f8b\u5982UTF-8\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728\u4ee3\u7801\u4e2d\u5904\u7406\u8bfb\u53d6\u5230\u7684doc\u6587\u4ef6\u5185\u5bb9\uff1f<\/strong><br \/>\u5728\u8bfb\u53d6doc\u6587\u4ef6\u540e\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528Python\u7684\u5b57\u7b26\u4e32\u5904\u7406\u65b9\u6cd5\u5bf9\u5185\u5bb9\u8fdb\u884c\u64cd\u4f5c\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u7684<code>split()<\/code>\u65b9\u6cd5\u5c06\u6bb5\u843d\u5206\u5272\u6210\u53e5\u5b50\uff0c\u6216\u8005\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u63d0\u53d6\u7279\u5b9a\u4fe1\u606f\u3002\u6b64\u5916\uff0c\u8fd8\u53ef\u4ee5\u5c06\u8bfb\u53d6\u5230\u7684\u5185\u5bb9\u4fdd\u5b58\u5230\u6570\u636e\u5e93\u6216\u8f93\u51fa\u5230\u5176\u4ed6\u683c\u5f0f\u7684\u6587\u4ef6\u4e2d\uff0c\u4ee5\u4fbf\u540e\u7eed\u4f7f\u7528\u548c\u5206\u6790\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python \u8bfb\u53d6 doc \u6587\u4ef6\u9700\u8981\u5b89\u88c5\u7684\u5305\u6709\uff1apython-docx\u3001pywin32\u3001docx2txt\u3002\u5176\u4e2d [&hellip;]","protected":false},"author":3,"featured_media":1108414,"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\/1108400"}],"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=1108400"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1108400\/revisions"}],"predecessor-version":[{"id":1108418,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1108400\/revisions\/1108418"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1108414"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1108400"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1108400"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1108400"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}