Traceback (most recent call last):
File "/Users/mac/Desktop/douDong/codeWorkSpace/doudong_building_demo/AI_PPT_DEMO/PPT/venv/lib/python3.12/site-packages/pptx/compat/init.py", line 10, in <module>
Container = collections.abc.Container
^^^^^^^^^^^^^^^
AttributeError: module 'collections' has no attribute 'abc'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/mac/Desktop/douDong/codeWorkSpace/doudong_building_demo/AI_PPT_DEMO/PPT/src/agent/PPTOperationAgent/tool/test/extract_ppt_elements.py", line 1, in <module>
from pptx import Presentation
File "/Users/mac/Desktop/douDong/codeWorkSpace/doudong_building_demo/AI_PPT_DEMO/PPT/venv/lib/python3.12/site-packages/pptx/init.py", line 14, in <module>
from pptx.api import Presentation # noqa
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/mac/Desktop/douDong/codeWorkSpace/doudong_building_demo/AI_PPT_DEMO/PPT/venv/lib/python3.12/site-packages/pptx/api.py", line 15, in <module>
from .package import Package
File "/Users/mac/Desktop/douDong/codeWorkSpace/doudong_building_demo/AI_PPT_DEMO/PPT/venv/lib/python3.12/site-packages/pptx/package.py", line 6, in <module>
from pptx.opc.package import OpcPackage
File "/Users/mac/Desktop/douDong/codeWorkSpace/doudong_building_demo/AI_PPT_DEMO/PPT/venv/lib/python3.12/site-packages/pptx/opc/package.py", line 11, in <module>
from pptx.compat import is_string, Mapping
File "/Users/mac/Desktop/douDong/codeWorkSpace/doudong_building_demo/AI_PPT_DEMO/PPT/venv/lib/python3.12/site-packages/pptx/compat/init.py", line 14, in <module>
Container = collections.Container
^^^^^^^^^^^^^^^^^^^^^
AttributeError: module 'collections' has no attribute 'Container'
直接按住ctrl或command,跳转到源码内部改成这样的
问题分析:
在 Python 3.10 及以上版本中,collections 模块确实不再直接包含 Container、Mapping 和 Sequence。相应的类现在被移动到了 collections.abc 中。虽然 python-pptx 库中包含了回退机制(try...except 语句),但它的兼容性代码未能完全适配 Python 3.10+,可能是由于版本的差异或者特定的环境问题。
解决方案:
我们可以尝试以下几个步骤来解决这个问题:
-
手动修改回退机制代码 :
确保回退机制代码正确无误,有时安装的
python-pptx版本可能会有一些问题。你可以根据以下步骤修改它。- 打开
pptx/compat/__init__.py文件(路径可能有所不同,取决于你的安装环境),找到以下代码块:
try: Container = collections.abc.Container Mapping = collections.abc.Mapping Sequence = collections.abc.Sequence except AttributeError: Container = collections.Container Mapping = collections.Mapping Sequence = collections.Sequence- 确保在
except AttributeError块中的代码能正确引用到collections.abc模块。你可以试着更新为:
try: Container = collections.abc.Container Mapping = collections.abc.Mapping Sequence = collections.abc.Sequence except AttributeError: from collections import abc Container = abc.Container Mapping = abc.Mapping Sequence = abc.Sequence - 打开