Python3.10虚拟环境下使用pip安装OOXML库失败如何解决
OOXML库安装失败问题排查与解决
问题复现
操作环境为Python 3.10,先安装virtualenv创建全新虚拟环境,执行env\Scripts\activate激活环境后,运行pip install ooxml命令安装OOXML库时出现报错,完整报错日志如下:
(env) PS C:\Users\ahmad\Desktop\XML-DOCX> pip install ooxml Collecting ooxml Using cached ooxml-0.2.1.tar.gz (4.3 kB) Preparing metadata (setup.py) ... done Building wheels for collected packages: ooxml Building wheel for ooxml (setup.py) ... error ERROR: Command errored out with exit status 1: command: 'C:\Users\ahmad\Desktop\XML-DOCX\env\Scripts\python.exe' -u -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '"'"'C:\\Users\\ahmad\\AppData\\Local\\Temp\\pip-install-r6q4bs_b\\ooxml_5b43566139ba4321a491b2ef7c9bdf16\\setup.py'"'"'; __file__='"'"'C:\\Users\\ahmad\\AppData\\Local\\Temp\\pip-install-r6q4bs_b\\ooxml_5b43566139ba4321a491b2ef7c9bdf16\\setup.py'"'"';f = getattr(tokenize, '"'"'open'"'"', open)(__file__) if os.path.exists(__file__) else io.StringIO('"'"'from setuptools import setup; setup()'"'"');code = f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' bdist_wheel -d 'C:\Users\ahmad\AppData\Local\Temp\pip-wheel-cw6u77ak' cwd: C:\Users\ahmad\AppData\Local\Temp\pip-install-r6q4bs_b\ooxml_5b43566139ba4321a491b2ef7c9bdf16\ Complete output (26 lines): running bdist_wheel running build running build_py creating build creating build\lib creating build\lib\ooxml copying ooxml\ooxmlbase.py -> build\lib\ooxml copying ooxml\spreadsheet.py -> build\lib\ooxml copying ooxml\__init__.py -> build\lib\ooxml C:\Users\ahmad\Desktop\XML-DOCX\env\lib\site-packages\setuptools\command\install.py:34: SetuptoolsDeprecationWarning: setup.py install is deprecated. Use build and pip and other standards-based tools. warnings.warn( installing to build\bdist.win-amd64\wheel running install running install_lib creating build\bdist.win-amd64 creating build\bdist.win-amd64\wheel creating build\bdist.win-amd64\wheel\ooxml copying build\lib\ooxml\ooxmlbase.py -> build\bdist.win-amd64\wheel\.\ooxml copying build\lib\ooxml\spreadsheet.py -> build\bdist.win-amd64\wheel\.\ooxml copying build\lib\ooxml\__init__.py -> build\bdist.win-amd64\wheel\.\ooxml running install_data creating build\bdist.win-amd64\wheel\ooxml-0.2.1.data creating build\bdist.win-amd64\wheel\ooxml-0.2.1.data\data creating build\bdist.win-amd64\wheel\ooxml-0.2.1.data\data\ooxml creating build\bdist.win-amd64\wheel\ooxml-0.2.1.data\data\ooxml\ooxml-templates error: can't copy 'ooxml\ooxml-templates\workbook.xlsx': doesn't exist or not a regular file ---------------------------------------- ERROR: Failed building wheel for ooxml Running setup.py clean for ooxml Failed to build ooxml Installing collected packages: ooxml Running setup.py install for ooxml ... error ERROR: Command errored out with exit status 1: command: 'C:\Users\ahmad\Desktop\XML-DOCX\env\Scripts\python.exe' -u -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '"'"'C:\\Users\\ahmad\\AppData\\Local\\Temp\\pip-install-r6q4bs_b\\ooxml_5b43566139ba4321a491b2ef7c9bdf16\\setup.py'"'"'; __file__='"'"'C:\\Users\\ahmad\\AppData\\Local\\Temp\\pip-install-r6q4bs_b\\ooxml_5b43566139ba4321a491b2ef7c9bdf16\\setup.py'"'"';f = getattr(tokenize, '"'"'open'"'"', open)(__file__) if os.path.exists(__file__) else io.StringIO('"'"'from setuptools import setup; setup()'"'"');code = f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record 'C:\Users\ahmad\AppData\Local\Temp\pip-record-44ixgoxr\install-record.txt' --single-version-externally-managed --compile --install-headers 'C:\Users\ahmad\Desktop\XML-DOCX\env\include\site\python3.10\ooxml' cwd: C:\Users\ahmad\AppData\Local\Temp\pip-install-r6q4bs_b\ooxml_5b43566139ba4321a491b2ef7c9bdf16\ Complete output (16 lines): running install C:\Users\ahmad\Desktop\XML-DOCX\env\lib\site-packages\setuptools\command\install.py:34: SetuptoolsDeprecationWarning: setup.py install is deprecated. Use build and pip and other standards-based tools. warnings.warn( running build running build_py creating build creating build\lib creating build\lib\ooxml copying ooxml\ooxmlbase.py -> build\lib\ooxml copying ooxml\spreadsheet.py -> build\lib\ooxml copying ooxml\__init__.py -> build\lib\ooxml running install_lib running install_data creating C:\Users\ahmad\Desktop\XML-DOCX\env\ooxml creating C:\Users\ahmad\Desktop\XML-DOCX\env\ooxml\ooxml-templates error: can't copy 'ooxml\ooxml-templates\workbook.xlsx': doesn't exist or not a regular file ---------------------------------------- ERROR: Command errored out with exit status 1: 'C:\Users\ahmad\Desktop\XML-DOCX\env\Scripts\python.exe' -u -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '"'"'C:\\Users\\ahmad\\AppData\\Local\\Temp\\pip-install-r6q4bs_b\\ooxml_5b43566139ba4321a491b2ef7c9bdf16\\setup.py'"'"'; __file__='"'"'C:\\Users\\ahmad\\AppData\\Local\\Temp\\pip-install-r6q4bs_b\\ooxml_5b43566139ba4321a491b2ef7c9bdf16\\setup.py'"'"';f = getattr(tokenize, '"'"'open'"'"', open)(__file__) if os.path.exists(__file__) else io.StringIO('"'"'from setuptools import setup; setup()'"'"');code = f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record 'C:\Users\ahmad\AppData\Local\Temp\pip-record-44ixgoxr\install-record.txt' --single-version-externally-managed --compile --install-headers 'C:\Users\ahmad\Desktop\XML-DOCX\env\include\site\python3.10\ooxml' Check the logs for full command output.
问题原因
- 直接报错原因是ooxml 0.2.1版本官方安装包存在打包缺陷,缺失了
ooxml/ooxml-templates/workbook.xlsx模板文件,导致安装过程中复制文件步骤失败。 - 该库最后一次更新是2013年,仅适配Python2.x版本,完全不兼容Python3.10环境,即使手动补全缺失文件完成安装,运行时也会触发大量语法错误。
解决方案
Python3环境下操作OOXML格式Office文档,推荐使用成熟的维护活跃的替代库:
- 操作Word .docx文件:使用
python-docx,安装命令:pip install python-docx - 操作Excel .xlsx文件:使用
openpyxl或pandas,安装命令:pip install openpyxl pandas - 操作PPT .pptx文件:使用
python-pptx,安装命令:pip install python-pptx
内容的提问来源于stack exchange,提问作者Ahmad
相关产品推荐
相关产品推荐

