无法在python中使用docx lib,无法从lxml导入etree

问题描述 投票:0回答:1

我在尝试使用 python 操作 MS 文档时遇到了这个问题。

我面临的错误是这样的:

Traceback (most recent call last):
  File "C:\Users\user\Desktop\Report Writer\open_new_doc.py", line 2, in <module>
    from docx import Document
  File "C:\Users\user\AppData\Local\Programs\Python\Python312\Lib\site-packages\docx\__init__.py", line 11, in <module>
    from docx.api import Document
  File "C:\Users\user\AppData\Local\Programs\Python\Python312\Lib\site-packages\docx\api.py", line 12, in <module>
    from docx.package import Package
  File "C:\Users\user\AppData\Local\Programs\Python\Python312\Lib\site-packages\docx\package.py", line 9, in <module>
    from docx.opc.package import OpcPackage
  File "C:\Users\user\AppData\Local\Programs\Python\Python312\Lib\site-packages\docx\opc\package.py", line 5, in <module>
    from docx.opc.part import PartFactory
  File "C:\Users\user\AppData\Local\Programs\Python\Python312\Lib\site-packages\docx\opc\part.py", line 7, in <module>
    from docx.opc.oxml import serialize_part_xml
  File "C:\Users\user\AppData\Local\Programs\Python\Python312\Lib\site-packages\docx\opc\oxml.py", line 8, in <module>
    from lxml import etree
ImportError: cannot import name 'etree' from 'lxml' (C:\Users\user\AppData\Local\Programs\Python\Python312\Lib\site-packages\lxml\__init__.py)
ImportError: cannot import name 'etree' from 'lxml' (C:\Users\user\AppData\Local\Programs\Python\Python312\Lib\site-packages\lxml\\__init\__.py)

有人可以向我解释发生了什么事以及我应该如何解决这个问题吗?编码方面相当新,刚刚学会了 python。

这是我的来源: https://github.com/chongjinisnotavailableisavailable/report_writer/blob/main/open_new_doc.py

我下载了 python-docx 库。重新安装lxml库。我没有安装 anaconda。

python v-:3.11.7
python-docx v-:1.1.0
lxml v-:4.9.3
typing-extensions v-:4.8.0

现在我只想打开 MSWord 文档并编写一些文本。

python lxml python-docx
1个回答
0
投票

从官方网站卸载并重新安装 3.12。现在可以了。谢谢你的帮助

© www.soinside.com 2019 - 2024. All rights reserved.