为何持续出现pywintypes.com_error: (-2147221005, 'Invalid class string')错误?
问题:Windows10+Python3.11.4下使用docx2pdf转换docx到PDF报错
环境:Windows 10系统,Python 3.11.4,尝试用docx2pdf库转换docx文件为PDF时,持续触发错误:pywintypes.com_error: (-2147221005, 'Invalid class string', None, None),同时调整导入语句时出现模块找不到的问题。
我的代码
import win32com.client #from win32 import _win32sysloader #from win32 import win32api #from docx2pdf import convert def translator(): print('reconverting') convert("C:\\Users\\John\\Downloads\\btab083 (1).docx", "C:\\Users\\John\\Downloads\\btab083 (1).pdf") print('success') translator()
完整错误信息
Traceback (most recent call last): File "C:\Users\John\AppData\Local\Programs\Python\Python311\Lib\site-packages\win32com\client\dynamic.py", line 84, in _GetGoodDispatch IDispatch = pythoncom.connect(IDispatch) pywintypes.com_error: (-2147221005, 'Invalid class string', None, None) During handling of the above exception, another exception occurred: Traceback (most recent call last): File "C:\Users\John\AppData\Local\Programs\Python\Python311\Scripts\translatorTest.py", line 55, in <module> translator() File "C:\Users\John\AppData\Local\Programs\Python\Python311\Scripts\translatorTest.py", line 53, in translator convert("C:\\Users\\John\\Downloads\\btab083 (1).docx") File "C:\Users\John\AppData\Local\Programs\Python\Python311\Lib\site-packages\docx2pdf\__init__.py", line 106, in convert return windows(paths, keep_active) File "C:\Users\John\AppData\Local\Programs\Python\Python311\Lib\site-packages\docx2pdf\__init__.py", line 19, in windows word = win32com.client.Dispatch("Word.Application") File "C:\Users\John\AppData\Local\Programs\Python\Python311\Lib\site-packages\win32com\client\__init__.py", line 118, in Dispatch dispatch, userName = dynamic._GetGoodDispatchAndUserName(dispatch, userName, clsctx) File "C:\Users\John\AppData\Local\Programs\Python\Python311\Lib\site-packages\win32com\client\dynamic.py", line 104, in _GetGoodDispatchAndUserName return (_GetGoodDispatch(IDispatch, clsctx), userName) File "C:\Users\John\AppData\Local\Programs\Python\Python311\Lib\site-packages\win32com\client\dynamic.py", line 86, in _GetGoodDispatch IDispatch = pythoncom.CoCreateInstance( pywintypes.com_error: (-2147221005, 'Invalid class string', None, None)
已尝试的操作
- 调整导入语句顺序(已注释),但出现
No module named 'pywintypes'或No module named '_win32sysloader'错误 - 执行过
pip install pywin32、python pywin32_postinstall.py -install等pywin32相关安装命令 - 尝试
pip install pywintypes,问题依旧
解决方案
核心问题分析
错误(-2147221005, 'Invalid class string')本质是系统无法找到Word的COM组件注册信息,常见原因包括:Office未正常安装、COM组件未注册、Python与Office位数不匹配,或pywin32安装不完整。
分步解决步骤
检查Office安装及位数匹配
- 确认安装正版Microsoft Office(WPS无法兼容docx2pdf依赖的Word COM调用)
- 查看Python位数:打开CMD执行
python -c "import platform; print(platform.architecture()[0])" - 查看Office位数:打开Word→文件→账户→关于Word,确保位数与Python完全一致,不一致则重装对应位数的Python或Office
重新注册Word的COM组件
- 以管理员身份打开CMD,根据Office位数执行对应命令:
- 64位Office:
cd "C:\Program Files\Microsoft Office\root\Office16" && regsvr32.exe /s WINWORD.EXE - 32位Office:
cd "C:\Program Files (x86)\Microsoft Office\root\Office16" && regsvr32.exe /s WINWORD.EXE
- 64位Office:
- 若找不到
Office16文件夹,替换为对应Office版本的文件夹(如Office15对应Office2013)
- 以管理员身份打开CMD,根据Office位数执行对应命令:
修复pywin32安装
- 先卸载现有pywin32:
pip uninstall -y pywin32 - 重新安装适配Python版本的pywin32:
pip install pywin32 - 必须运行postinstall脚本:找到Python安装目录下的
Scripts/pywin32_postinstall.py,以管理员身份执行python pywin32_postinstall.py -install
- 先卸载现有pywin32:
测试COM组件可用性
先验证能否正常调用Word COM组件,再执行转换:import win32com.client try: word = win32com.client.Dispatch("Word.Application") word.Quit() print("Word COM组件调用正常") except Exception as e: print(f"调用失败:{e}")若此步骤成功,再运行docx2pdf的转换代码。
替代方案(绕开docx2pdf)
直接用win32com实现转换,避免依赖docx2pdf:import win32com.client import os def docx_to_pdf(docx_path, pdf_path): word = win32com.client.Dispatch("Word.Application") word.Visible = False # 后台运行,不显示Word窗口 try: doc = word.Documents.Open(docx_path) doc.SaveAs(pdf_path, FileFormat=17) # 17是PDF格式的对应编码 doc.Close() print("转换成功") except Exception as e: print(f"转换失败:{e}") finally: word.Quit() # 调用转换函数 docx_to_pdf("C:\\Users\\John\\Downloads\\btab083 (1).docx", "C:\\Users\\John\\Downloads\\btab083 (1).pdf")
内容的提问来源于stack exchange,提问作者tthheemmaannii
相关产品推荐
相关产品推荐

