【Python】windows下读取doc报错<unknown>.Text

这篇博客探讨了在Python中使用win32com进行Word文档操作时遇到的常见错误,包括'AttributeError:<unknown>.Open'和'AttributeError:<unknown>.Text'。解决方案包括在代码中加入time.sleep(1)以确保操作完成,以及正确处理文件路径和导入pythoncom以解决'CoInitialize'错误。此外,还提到了'(-2147352567, '发生意外。')'错误,可能由于文件路径错误,建议使用绝对路径。最后,针对'ImportError:DLL load failed while importing win32api',推荐卸载并重新安装特定版本的pywin32。

“AttributeError:<unknown>.Open”

“AttributeError:<unknown>.Text”

这两个问题都可以使用以下方法:

from win32com.client import Dispatch
import time
def read_doc_win(path):
    content = []
    word = Dispatch('Word.Application')  # 打开word应用程序
    word.Visible = 0  # 后台运行,不显示
    word.DisplayAlerts = 0  # 不警告
    doc = word.Documents.Open(FileName=path, Encoding='utf-8')
    for para in doc.paragraphs:
        s = para.Range.Text.strip() + "\n"
        content.append(s)
    doc.Close()
    word.Quit
    time.sleep(1)
    return content

关键部分就是加入了:

time.sleep(1)

原因是work处理还没结束,下一个操作就又开始了

解决方案两个:
(1)购买性能更好的CPU;
(2)在代码中加入time.sleep(1),休息1秒,就OK了。

(-2147221008, ‘尚未调用 CoInitialize。’, None, None)

import pythoncom,这个是win32com里自带的
在初始化的地即方在word = Dispatch("Word.Application")前后前后加上 pythoncom.CoInitialize()即可解决该报错

pythoncom.CoInitialize()#加上的
word = Dispatch("Word.Application")
pythoncom.CoInitialize()#加上的

(-2147352567, ‘发生意外。’, (0, ‘Kingsoft WPS’, ‘文档打开失败。’, ‘’’, 3010, -786427), None)

原因不在于程序尚未退出,而在于使用的文件路径方式不对:使用相对路径报错,更换为绝对路径就ok了

ImportError: DLL load failed while importing win32api: 找不到指定的模块。

先卸载pywin32、pypiwin32
安装pywin32==225:pip install pywin32==225
D:\GitProjects\REQManagement\.venv\RRM_env\Scripts\python.exe -X pycache_prefix=C:\Users\kotei\AppData\Local\JetBrains\PyCharmCE2024.3\cpython-cache "D:/Program Files/JetBrains/PyCharm Community Edition 2024.3.4/plugins/python-ce/helpers/pydev/pydevd.py" --multiprocess --qt-support=auto --client 127.0.0.1 --port 59309 --file "D:/Program Files/JetBrains/PyCharm Community Edition 2024.3.4/plugins/python-ce/helpers/pycharm/_jb_pytest_runner.py" --path D:\GitProjects\REQManagement\DDE\kotei_web_server\utils\test.py Testing started at 15:01 ... 已连接到 pydev 调试器(内部版本号 243.25659.43)Launching pytest with arguments D:\GitProjects\REQManagement\DDE\kotei_web_server\utils\test.py --no-header --no-summary -q in D:\GitProjects\REQManagement\DDE\kotei_web_server\utils ============================= test session starts ============================= collecting ... collected 8 items test.py::OfficeLocatorTest::test_clear_marks test.py::OfficeLocatorTest::test_excel_cell_locating test.py::OfficeLocatorTest::test_office_location_api test.py::OfficeLocatorTest::test_open_close_documents test.py::OfficeLocatorTest::test_picture_locating test.py::OfficeLocatorTest::test_table_locating test.py::OfficeLocatorTest::test_text_paragraph_locating test.py::OfficeLocatorTest::test_window_positioning ============================= 8 errors in 11.79s ============================== ERROR [ 12%]2025-09-16 15:02:03 - [MainThread] - INFO - (test.py:112) $ setUpClass) ::: === 准备测试环境 === test setup failed cls = <class 'kotei_web_server.utils.test.OfficeLocatorTest'> @classmethod def setUpClass(cls): """创建测试文档并初始化""" logging.info("=== 准备测试环境 ===") > cls.word_path, cls.excel_path = create_test_documents() test.py:113: _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ test.py:52: in create_test_documents shapes.AddPicture( _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ self = <COMObject <unknown>> FileName = 'D:\\GitProjects\\REQManagement\\DDE\\kotei_web_server\\utils\\test.py' LinkToFile = False, SaveWithDocument = True, Left = 100, Top = 100, Width = 100 Height = 100, Anchor = <PyOleMissing object at 0x000001B13AAF4920> > ??? E pywintypes.com_error: (-2147352567, '发生意外。', (0, None, None, None, 0, -2147467259), None) <COMObject <unknown>>:3: com_error ERROR [ 25%] test setup failed cls = <class 'kotei_web_server.utils.test.OfficeLocatorTest'> @classmethod def setUpClass(cls): """创建测试文档并初始化""" logging.info("=== 准备测试环境 ===") > cls.word_path, cls.excel_path = create_test_documents() test.py:113: _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ test.py:52: in create_test_documents shapes.AddPicture( _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ self = <COMObject <unknown>> FileName = 'D:\\GitProjects\\REQManagement\\DDE\\kotei_web_server\\utils\\test.py' LinkToFile = False, SaveWithDocument = True, Left = 100, Top = 100, Width = 100 Height = 100, Anchor = <PyOleMissing object at 0x000001B13AAF4920> > ??? E pywintypes.com_error: (-2147352567, '发生意外。', (0, None, None, None, 0, -2147467259), None) <COMObject <unknown>>:3: com_error ERROR [ 37%] test setup failed cls = <class 'kotei_web_server.utils.test.OfficeLocatorTest'> @classmethod def setUpClass(cls): """创建测试文档并初始化""" logging.info("=== 准备测试环境 ===") > cls.word_path, cls.excel_path = create_test_documents() test.py:113: _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ test.py:52: in create_test_documents shapes.AddPicture( _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ self = <COMObject <unknown>> FileName = 'D:\\GitProjects\\REQManagement\\DDE\\kotei_web_server\\utils\\test.py' LinkToFile = False, SaveWithDocument = True, Left = 100, Top = 100, Width = 100 Height = 100, Anchor = <PyOleMissing object at 0x000001B13AAF4920> > ??? E pywintypes.com_error: (-2147352567, '发生意外。', (0, None, None, None, 0, -2147467259), None) <COMObject <unknown>>:3: com_error ERROR [ 50%] test setup failed cls = <class 'kotei_web_server.utils.test.OfficeLocatorTest'> @classmethod def setUpClass(cls): """创建测试文档并初始化""" logging.info("=== 准备测试环境 ===") > cls.word_path, cls.excel_path = create_test_documents() test.py:113: _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ test.py:52: in create_test_documents shapes.AddPicture( _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ self = <COMObject <unknown>> FileName = 'D:\\GitProjects\\REQManagement\\DDE\\kotei_web_server\\utils\\test.py' LinkToFile = False, SaveWithDocument = True, Left = 100, Top = 100, Width = 100 Height = 100, Anchor = <PyOleMissing object at 0x000001B13AAF4920> > ??? E pywintypes.com_error: (-2147352567, '发生意外。', (0, None, None, None, 0, -2147467259), None) <COMObject <unknown>>:3: com_error ERROR [ 62%] test setup failed cls = <class 'kotei_web_server.utils.test.OfficeLocatorTest'> @classmethod def setUpClass(cls): """创建测试文档并初始化""" logging.info("=== 准备测试环境 ===") > cls.word_path, cls.excel_path = create_test_documents() test.py:113: _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ test.py:52: in create_test_documents shapes.AddPicture( _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ self = <COMObject <unknown>> FileName = 'D:\\GitProjects\\REQManagement\\DDE\\kotei_web_server\\utils\\test.py' LinkToFile = False, SaveWithDocument = True, Left = 100, Top = 100, Width = 100 Height = 100, Anchor = <PyOleMissing object at 0x000001B13AAF4920> > ??? E pywintypes.com_error: (-2147352567, '发生意外。', (0, None, None, None, 0, -2147467259), None) <COMObject <unknown>>:3: com_error ERROR [ 75%] test setup failed cls = <class 'kotei_web_server.utils.test.OfficeLocatorTest'> @classmethod def setUpClass(cls): """创建测试文档并初始化""" logging.info("=== 准备测试环境 ===") > cls.word_path, cls.excel_path = create_test_documents() test.py:113: _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ test.py:52: in create_test_documents shapes.AddPicture( _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ self = <COMObject <unknown>> FileName = 'D:\\GitProjects\\REQManagement\\DDE\\kotei_web_server\\utils\\test.py' LinkToFile = False, SaveWithDocument = True, Left = 100, Top = 100, Width = 100 Height = 100, Anchor = <PyOleMissing object at 0x000001B13AAF4920> > ??? E pywintypes.com_error: (-2147352567, '发生意外。', (0, None, None, None, 0, -2147467259), None) <COMObject <unknown>>:3: com_error ERROR [ 87%] test setup failed cls = <class 'kotei_web_server.utils.test.OfficeLocatorTest'> @classmethod def setUpClass(cls): """创建测试文档并初始化""" logging.info("=== 准备测试环境 ===") > cls.word_path, cls.excel_path = create_test_documents() test.py:113: _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ test.py:52: in create_test_documents shapes.AddPicture( _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ self = <COMObject <unknown>> FileName = 'D:\\GitProjects\\REQManagement\\DDE\\kotei_web_server\\utils\\test.py' LinkToFile = False, SaveWithDocument = True, Left = 100, Top = 100, Width = 100 Height = 100, Anchor = <PyOleMissing object at 0x000001B13AAF4920> > ??? E pywintypes.com_error: (-2147352567, '发生意外。', (0, None, None, None, 0, -2147467259), None) <COMObject <unknown>>:3: com_error ERROR [100%] test setup failed cls = <class 'kotei_web_server.utils.test.OfficeLocatorTest'> @classmethod def setUpClass(cls): """创建测试文档并初始化""" logging.info("=== 准备测试环境 ===") > cls.word_path, cls.excel_path = create_test_documents() test.py:113: _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ test.py:52: in create_test_documents shapes.AddPicture( _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ self = <COMObject <unknown>> FileName = 'D:\\GitProjects\\REQManagement\\DDE\\kotei_web_server\\utils\\test.py' LinkToFile = False, SaveWithDocument = True, Left = 100, Top = 100, Width = 100 Height = 100, Anchor = <PyOleMissing object at 0x000001B13AAF4920> > ??? E pywintypes.com_error: (-2147352567, '发生意外。', (0, None, None, None, 0, -2147467259), None) <COMObject <unknown>>:3: com_error 进程已结束,退出代码为 1
最新发布
09-17
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值