{"id":221,"date":"2025-06-30T18:03:35","date_gmt":"2025-06-30T10:03:35","guid":{"rendered":"https:\/\/www.idcyz.com\/blog\/?p=221"},"modified":"2025-06-30T18:03:35","modified_gmt":"2025-06-30T10:03:35","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8python%e5%a4%84%e7%90%86-doc%e6%96%87%e4%bb%b6%e5%86%85%e5%ae%b9%e5%b9%b6%e8%bf%9b%e8%a1%8c%e8%bd%ac%e7%a0%81%e7%9a%84%e6%9c%80%e6%96%b0%e6%96%b9%e6%b3%95%e6%98%af","status":"publish","type":"post","link":"https:\/\/www.idcyz.com\/blog\/221.html","title":{"rendered":"\u5982\u4f55\u4f7f\u7528Python\u5904\u7406.doc\u6587\u4ef6\u5185\u5bb9\u5e76\u8fdb\u884c\u8f6c\u7801\u7684\u6700\u65b0\u65b9\u6cd5\u662f\u4ec0\u4e48"},"content":{"rendered":"<p><p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/www.idcyz.com\/blog\/wp-content\/uploads\/2025\/06\/A4HcfFPpv9.jpg\" alt=\"\u5982\u4f55\u4f7f\u7528Python\u5904\u7406.doc\u6587\u4ef6\u5185\u5bb9\u5e76\u8fdb\u884c\u8f6c\u7801\u7684\u6700\u65b0\u65b9\u6cd5\u662f\u4ec0\u4e48\" title=\"\u5982\u4f55\u4f7f\u7528Python\u5904\u7406.doc\u6587\u4ef6\u5185\u5bb9\u5e76\u8fdb\u884c\u8f6c\u7801\u7684\u6700\u65b0\u65b9\u6cd5\u662f\u4ec0\u4e48\"><\/p>\n<p><h3>1. Python-docx\u5e93<\/h3>\n<\/p>\n<p>Python-docx\u662f\u4e00\u4e2a\u7528\u4e8e\u521b\u5efa\u548c\u66f4\u65b0Microsoft Word 2007\u53ca\u66f4\u9ad8\u7248\u672c.docx\u6587\u4ef6\u7684\u5e93\u3002\u4f7f\u7528\u6b64\u5e93\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u8bbf\u95ee\u6587\u6863\u7684\u5185\u5bb9\u5e76\u8fdb\u884c\u8f6c\u7801\u3002<\/p>\n<p><p>\u5b83\u5141\u8bb8\u6211\u4eec\u8bfb\u53d6\u548c\u5199\u5165\u6587\u6863\u7684\u6587\u672c\uff0c\u4ee5\u53ca\u683c\u5f0f\u5316\u6837\u5f0f\u3002\u6211\u4eec\u53ef\u4ee5\u63d0\u53d6\u6587\u6863\u4e2d\u7684\u6bb5\u843d\u3001\u8868\u683c\u7b49\uff0c\u6b64\u5916\u8fd8\u652f\u6301\u5c06\u6587\u672c\u7f16\u7801\u4e3aUTF-8\u7b49\u683c\u5f0f\uff0c\u975e\u5e38\u9002\u5408\u9700\u8981\u8f6c\u6362\u7f16\u7801\u7684\u573a\u666f\u3002\u8981\u4f7f\u7528python-docx\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5\uff1a  <\/p>\n<p><pre><code>pip install python-docx<\/code><\/pre>\n<\/p>\n<\/p>\n<p>\n<h3>2. PyWin32\u5e93<\/h3>\n<\/p>\n<p>PyWin32\u662f\u4e00\u4e2aWindows\u5e73\u53f0\u4e0a\u7684Python\u5e93\uff0c\u5141\u8bb8\u6211\u4eec\u4f7f\u7528COM\u5bf9\u8c61\u4e0eMicrosoft Office\u5e94\u7528\u7a0b\u5e8f\u8fdb\u884c\u4ea4\u4e92\u3002\u901a\u8fc7\u8fd9\u4e2a\u5e93\uff0c\u6211\u4eec\u53ef\u4ee5\u8bfb\u53d6\u3001\u4fee\u6539\u548c\u4fdd\u5b58Word\u6587\u4ef6\uff0c\u4ee5\u5b9e\u73b0\u5185\u5bb9\u7684\u8f6c\u7801\u3002<\/p>\n<p><p>\u5b89\u88c5PyWin32\u4e5f\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u6267\u884c\u4ee5\u4e0b\u547d\u4ee4\u5373\u53ef\uff1a  <\/p>\n<p><pre><code>pip install pywin32<\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528PyWin32\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u6253\u5f00Word\u5e94\u7528\u7a0b\u5e8f\uff0c\u52a0\u8f7d\u6587\u6863\uff0c\u7136\u540e\u8bfb\u53d6\u6240\u9700\u7684\u6587\u672c\u5185\u5bb9\uff0c\u5e76\u5c06\u5176\u8f6c\u7801\u4e3a\u6240\u9700\u683c\u5f0f\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u8fd9\u4e2a\u5e93\u53ea\u9002\u7528\u4e8eWindows\u73af\u5883\u3002<\/p>\n<\/p>\n<p>\n<h3>3. pypiwin32\u5e93<\/h3>\n<\/p>\n<p>pypiwin32\u662fPython\u7684\u4e00\u4e2a\u6269\u5c55\u5e93\uff0c\u5b83\u4e5f\u662f\u7528\u4e8e\u4e0eWindows\u5e94\u7528\u7a0b\u5e8f\u95f4\u7684\u4ea4\u4e92\u3002\u5b83\u5e76\u4e0d\u80fd\u76f4\u63a5\u5904\u7406.doc\u6587\u4ef6\uff0c\u4f46\u53ef\u4ee5\u901a\u8fc7\u4e0e\u5176\u4ed6\u5e93\u7ed3\u5408\u4f7f\u7528\u6765\u5b8c\u6210\u6587\u4ef6\u64cd\u4f5c\u548c\u8f6c\u7801\u3002<\/p>\n<p><p>\u5982\u679c\u4f60\u5728\u5904\u7406.doc\u6587\u4ef6\u65f6\u9047\u5230\u56f0\u96be\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u6b64\u5e93\u4e0e\u5176\u4ed6\u5e93\u5982python-docx\u7ec4\u5408\u4f7f\u7528\u3002\u5b89\u88c5\u547d\u4ee4\u4e3a\uff1a  <\/p>\n<p><pre><code>pip install pypiwin32<\/code><\/pre>\n<\/p>\n<\/p>\n<p>\n<h3>4. comtypes\u5e93<\/h3>\n<\/p>\n<p>comtypes\u662f\u4e00\u4e2a\u8f83\u4e3a\u9ad8\u7ea7\u7684Python\u5e93\uff0c\u5b83\u80fd\u591f\u4e0eWindows COM\u63a5\u53e3\u8fdb\u884c\u66f4\u590d\u6742\u7684\u4ea4\u4e92\uff0c\u9002\u5408\u9700\u8981\u6df1\u5165\u8bbf\u95eeWord\u6587\u6863\u5185\u5bb9\u7684\u60c5\u51b5\u3002<\/p>\n<p><p>\u901a\u8fc7comtypes\uff0c\u7528\u6237\u53ef\u4ee5\u6253\u5f00Word\u5e94\u7528\u7a0b\u5e8f\uff0c\u8bbf\u95ee\u6587\u6863\u7684\u6bcf\u4e00\u4e2a\u7ec6\u8282\uff0c\u4ece\u6587\u672c\u5230\u56fe\u5f62\uff0c\u518d\u5230\u8868\u683c\u5747\u53ef\u8fdb\u884c\u8bfb\u53d6\u548c\u64cd\u4f5c\u3002\u5b89\u88c5\u65b9\u5f0f\u5982\u4e0b\uff1a  <\/p>\n<p><pre><code>pip install comtypes<\/code><\/pre>\n<\/p>\n<\/p>\n<p>\n<h3>5. Textract\u5e93<\/h3>\n<\/p>\n<p>Textract\u662f\u4e00\u4e2a\u5f3a\u5927\u4e14\u591a\u529f\u80fd\u7684\u5e93\uff0c\u5b83\u4e0d\u4ec5\u652f\u6301Word\u6587\u6863\u7684\u8bfb\u53d6\uff0c\u8fd8\u80fd\u591f\u5904\u7406PDF\u3001\u56fe\u50cf\u7b49\u6587\u4ef6\u683c\u5f0f\uff0c\u65b9\u4fbf\u7528\u6237\u63d0\u53d6\u6587\u672c\u3002<\/p>\n<p><p>\u6587\u672c\u5c06\u5728\u8bfb\u53d6\u540e\u81ea\u52a8\u8fdb\u884c\u7f16\u7801\u5904\u7406\uff0c\u7528\u6237\u53ea\u9700\u8981\u5173\u6ce8\u5982\u4f55\u83b7\u53d6\u6240\u9700\u4fe1\u606f\u3002\u5b89\u88c5\u65b9\u6cd5\u5982\u4e0b\uff1a  <\/p>\n<p><pre><code>pip install textract<\/code><\/pre>\n<\/p>\n<\/p>\n<p>\n<h3>6. PDFMiner\u5e93<\/h3>\n<\/p>\n<p>\u867d\u7136PDFMiner\u4e3b\u8981\u7528\u4e8ePDF\u6587\u6863\uff0c\u4f46\u5b83\u5728\u5904\u7406\u6587\u6863\u5185\u5bb9\u7684\u8bfb\u53d6\u548c\u8f6c\u7801\u4e0a\u4e5f\u5f88\u6709\u5e2e\u52a9\u3002\u5982\u679c\u4f60\u6709PDF\u6587\u4ef6\u9700\u8981\u8f6c\u7801\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u6b64\u5e93\u3002<\/p>\n<p><p>\u4e0e\u5176\u4ed6\u5e93\u76f8\u6bd4\uff0c\u5b83\u63d0\u4f9b\u4e86\u66f4\u6df1\u5165\u7684\u5185\u5bb9\u89e3\u6790\u65b9\u5f0f\uff0c\u4ee5\u5e2e\u52a9\u7528\u6237\u83b7\u5f97\u9ad8\u8d28\u91cf\u7684\u6587\u672c\u3002\u5b89\u88c5\u547d\u4ee4\u5982\u4e0b\uff1a  <\/p>\n<p><pre><code>pip install pdfminer.six<\/code><\/pre>\n<\/p>\n<\/p>\n<p>\n<h3>7. docx2txt\u5e93<\/h3>\n<\/p>\n<p>docx2txt\u662f\u4e00\u4e2a\u7b80\u5355\u6613\u7528\u7684\u5e93\uff0c\u4e13\u95e8\u7528\u4e8e\u4ece.docx\u6587\u4ef6\u4e2d\u63d0\u53d6\u6587\u672c\u548c\u56fe\u50cf\u3002\u5b83\u80fd\u591f\u9ad8\u6548\u4e14\u51c6\u786e\u5730\u8f93\u51fa\u6587\u672c\uff0c\u9002\u5408\u5feb\u901f\u9700\u8981\u6587\u6863\u5185\u5bb9\u7684\u7528\u6237\u3002<\/p>\n<p><p>\u7528\u6237\u53ea\u9700\u8c03\u7528\u4e00\u4e2a\u7b80\u5355\u7684\u547d\u4ee4\uff0c\u5c31\u80fd\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u5e76\u8f93\u51fa\u3002\u5176\u5b89\u88c5\u65b9\u5f0f\u4e3a\uff1a  <\/p>\n<p><pre><code>pip install docx2txt<\/code><\/pre>\n<\/p>\n<\/p>\n<p>\n<h3>8. OpenPyXL\u5e93<\/h3>\n<\/p>\n<p>\u867d\u7136OpenPyXL\u662f\u9488\u5bf9Excel\u6587\u6863\u7684\u5e93\uff0c\u4f46\u5728\u5904\u7406\u4e0eWord\u76f8\u5173\u7684\u6587\u672c\u6587\u4ef6\u65f6\uff0c\u7ed3\u5408Python\u7684\u5176\u4ed6\u5e93\u4f7f\u7528\u65f6\u4e5f\u80fd\u5b9e\u73b0\u4e00\u5b9a\u7684\u6587\u672c\u5904\u7406\u548c\u8f6c\u7801\u529f\u80fd\u3002<\/p>\n<p><p>\u5728\u9700\u8981\u8de8\u6587\u6863\u5de5\u4f5c\u65f6\uff0cOpenPyXL\u53ef\u80fd\u4f1a\u662f\u4e00\u4e2a\u8f85\u52a9\u5de5\u5177\u3002\u5b89\u88c5\u8fd9\u4e2a\u5e93\u7684\u65b9\u6cd5\u4e3a\uff1a  <\/p>\n<p><pre><code>pip install openpyxl<\/code><\/pre>\n<\/p>\n<\/p>\n<p>\n<h3>9. Tika\u5e93<\/h3>\n<\/p>\n<p>Tika\u662f\u4e00\u4e2a\u5185\u5bb9\u5206\u6790\u5e93\uff0c\u5b83\u652f\u6301\u591a\u79cd\u6587\u6863\u683c\u5f0f\u7684\u5185\u5bb9\u63d0\u53d6\uff0c\u5305\u62ecWord\u6587\u4ef6\u3002\u4f7f\u7528Tika\uff0c\u6211\u4eec\u53ef\u4ee5\u83b7\u53d6\u6587\u6863\u4e2d\u7684\u6240\u6709\u6587\u672c\u5e76\u8fdb\u884c\u8f6c\u7801\u64cd\u4f5c\u3002<\/p>\n<p><p>\u5982\u679c\u4f60\u9700\u8981\u5904\u7406\u4e0d\u540c\u683c\u5f0f\u7684\u6587\u4ef6\uff0cTika\u53ef\u80fd\u662f\u4e2a\u4e0d\u9519\u7684\u9009\u62e9\u3002\u5b89\u88c5\u547d\u4ee4\u5982\u4e0b\uff1a  <\/p>\n<p><pre><code>pip install tika<\/code><\/pre>\n<\/p>\n<\/p>\n<p>\n<h3>10. \u8bbe\u7f6e\u7f16\u7801\u8f6c\u6362\u7684Python\u4ee3\u7801\u793a\u4f8b<\/h3>\n<\/p>\n<p>\u4ee5\u4e0b\u662f\u4f7f\u7528python-docx\u8bfb\u53d6.docx\u6587\u6863\u5e76\u8f6c\u7801\u4e3aUTF-8\u7684\u793a\u4f8b\u4ee3\u7801\uff1a  <\/p>\n<p><pre><code>  <\/p>\r\n<p>from docx import Document  <\/p>\r\n\r\n<p># load the document  <\/p>\r\n<p>doc = Document('example.docx')  <\/p>\r\n\r\n<p># read paragraphs and convert to UTF-8  <\/p>\r\n<p>text = []  <\/p>\r\n<p>for para in doc.paragraphs:  <\/p>\r\n<p>    text.append(para.text.encode('utf-8'))  <\/p>\r\n\r\n<p># write to utf-8 file  <\/p>\r\n<p>with open('output.txt', 'wb') as f:  <\/p>\r\n<p>    for item in text:  <\/p>\r\n<p>        f.write(item + b\"\\n\")  <\/p>\r\n<p><\/code><\/pre>\n<\/p>\n<p>\u8fd9\u6837\u53ef\u4ee5\u5c06Word\u6587\u6863\u4e2d\u7684\u6587\u672c\u5185\u5bb9\u63d0\u53d6\u5e76\u4fdd\u5b58\u4e3aUTF-8\u7f16\u7801\u7684\u6587\u672c\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p>\n<h3>\u95ee\u7b54\u73af\u8282<\/h3>\n<\/p>\n<p><p><b>\u53ef\u4ee5\u7528\u54ea\u4e9b\u5e93\u5b9e\u73b0Python\u5904\u7406.doc\u6587\u4ef6\u4e2d\u7684\u5185\u5bb9\u8f6c\u7801\uff1f<\/b>  <\/p>\n<p>\u53ef\u4ee5\u4f7f\u7528\u591a\u4e2a\u5e93\u6765\u5b9e\u73b0\u8fd9\u4e2a\u529f\u80fd\uff0c\u63a8\u8350\u7684\u5e93\u6709\uff1apython-docx\u3001PyWin32\u3001pypiwin32\u3001comtypes\u3001Textract\u7b49\u3002\u8fd9\u4e9b\u5e93\u5404\u6709\u7279\u70b9\uff0c\u9002\u5408\u4e0d\u540c\u7684\u4f7f\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<p><p><b>\u5982\u4f55\u9009\u62e9\u5408\u9002\u7684\u5e93\u6765\u5904\u7406\u6587\u6863\u8f6c\u7801\uff1f<\/b>  <\/p>\n<p>\u9009\u62e9\u5408\u9002\u7684\u5e93\u4e3b\u8981\u53d6\u51b3\u4e8e\u4f60\u7684\u64cd\u4f5c\u7cfb\u7edf\u548c\u5177\u4f53\u9700\u6c42\u3002\u5982\u679c\u5728Windows\u73af\u5883\u4e2d\uff0c\u53ef\u4ee5\u4f18\u5148\u8003\u8651PyWin32\u548ccomtypes\uff0c\u5982\u679c\u9700\u8981\u591a\u5e73\u53f0\u652f\u6301\uff0cpython-docx\u548cTextract\u662f\u4e0d\u9519\u7684\u9009\u62e9\u3002<\/p>\n<\/p>\n<p><p><b>\u80fd\u4e0d\u80fd\u63d0\u4f9b\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\u6765\u6f14\u793a\u5982\u4f55\u8bfb\u53d6\u5185\u5bb9\u5e76\u8f6c\u7801\uff1f<\/b>  <\/p>\n<p>\u5f53\u7136\u53ef\u4ee5\uff0c\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u4f7f\u7528python-docx\u5e93\u8bfb\u53d6.docx\u6587\u4ef6\u7684\u5185\u5bb9\u5e76\u8f6c\u7801\u4e3aUTF-8\u683c\u5f0f\u7684\u4ee3\u7801\uff1a  <\/p>\n<p><pre><code>  <\/p>\r\n<p>from docx import Document  <\/p>\r\n\r\n<p>doc = Document('example.docx')  <\/p>\r\n<p>text = [para.text.encode('utf-8') for para in doc.paragraphs]  <\/p>\r\n\r\n<p>with open('output.txt', 'wb') as f:  <\/p>\r\n<p>    for item in text:  <\/p>\r\n<p>        f.write(item + b\"\\n\")  <\/p>\r\n<p><\/code><\/pre>\n<\/p>\n<p>\u8be5\u793a\u4f8b\u6f14\u793a\u4e86\u5982\u4f55\u8bfb\u53d6Word\u6587\u6863\uff0c\u5e76\u5c06\u5176\u5185\u5bb9\u4fdd\u5b58\u4e3aUTF-8\u683c\u5f0f\u7684\u6587\u672c\u6587\u4ef6\u3002<\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>1. Python-docx\u5e93 Python-docx\u662f\u4e00\u4e2a\u7528\u4e8e\u521b\u5efa\u548c\u66f4\u65b0Microsoft Word 2007\u53ca\u66f4\u9ad8\u7248\u672c.docx\u6587\u4ef6\u7684\u5e93\u3002\u4f7f\u7528\u6b64\u5e93\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u8bbf\u95ee\u6587\u6863\u7684\u5185\u5bb9\u5e76\u8fdb\u884c\u8f6c\u7801\u3002 \u5b83\u5141\u8bb8\u6211\u4eec\u8bfb\u53d6\u548c\u5199\u5165\u6587\u6863\u7684\u6587\u672c\uff0c\u4ee5\u53ca\u683c\u5f0f\u5316\u6837\u5f0f\u3002&#8230;<\/p>\n","protected":false},"author":1,"featured_media":222,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[126,12,127],"topic":[],"class_list":["post-221","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-jiushu","tag-docx","tag-python","tag-127"],"_links":{"self":[{"href":"https:\/\/www.idcyz.com\/blog\/wp-json\/wp\/v2\/posts\/221","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.idcyz.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.idcyz.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.idcyz.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.idcyz.com\/blog\/wp-json\/wp\/v2\/comments?post=221"}],"version-history":[{"count":1,"href":"https:\/\/www.idcyz.com\/blog\/wp-json\/wp\/v2\/posts\/221\/revisions"}],"predecessor-version":[{"id":224,"href":"https:\/\/www.idcyz.com\/blog\/wp-json\/wp\/v2\/posts\/221\/revisions\/224"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.idcyz.com\/blog\/wp-json\/wp\/v2\/media\/222"}],"wp:attachment":[{"href":"https:\/\/www.idcyz.com\/blog\/wp-json\/wp\/v2\/media?parent=221"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.idcyz.com\/blog\/wp-json\/wp\/v2\/categories?post=221"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.idcyz.com\/blog\/wp-json\/wp\/v2\/tags?post=221"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.idcyz.com\/blog\/wp-json\/wp\/v2\/topic?post=221"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}