Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

优化cannot import name 'OxmlElement' from 'docx.oxml'问题 #233

Merged
merged 1 commit into from
Jan 7, 2024

Conversation

Syske
Copy link
Contributor

@Syske Syske commented Oct 11, 2023

由于python-docx更新,导致导包报错;

from docx.oxml import OxmlElement, parse_xml, register_element_cls

错误信息:

Traceback (most recent call last):
  File "D:\workspace\learning\python-script\pdf2wod.py", line 2, in <module>
    from pdf2docx import parse
  File "D:\workspace\dev-tools\python\python39\lib\site-packages\pdf2docx\__init__.py", line 1, in <module>
    from .converter import Converter
  File "D:\workspace\dev-tools\python\python39\lib\site-packages\pdf2docx\converter.py", line 9, in <module>
    from .page.Page import Page
  File "D:\workspace\dev-tools\python\python39\lib\site-packages\pdf2docx\page\Page.py", line 47, in <module>
    from ..layout.Sections import Sections
  File "D:\workspace\dev-tools\python\python39\lib\site-packages\pdf2docx\layout\Sections.py", line 9, in <module>
    from ..common.docx import reset_paragraph_format
  File "D:\workspace\dev-tools\python\python39\lib\site-packages\pdf2docx\common\docx.py", line 7, in <module>
    from docx.oxml import OxmlElement, parse_xml, register_element_cls
ImportError: cannot import name 'OxmlElement' from 'docx.oxml' (D:\workspace\dev-tools\python\python39\lib\site-packages\docx\oxml\__init__.py)

@dothinking
Copy link
Contributor

感谢,这是 python-docx 从 v0.8.11 升级到 v1.0.0 出现的问题,随后作者在 v1.0.1中做了修复,python-docx#1256

建议用 try-except 同时考虑两种导入方式,以便兼容 1.0.0 和 其他版本。

try:
    # python-docx <= 0.8.11 or python-docx > 1.0.0
    from docx.oxml import OxmlElement, parse_xml, register_element_cls
except ImportError:
    # python-docx >= 1.0.0
    from docx.oxml.parser import OxmlElement, parse_xml, register_element_cls

@Syske
Copy link
Contributor Author

Syske commented Jan 5, 2024 via email

@dothinking dothinking merged commit bdb09fb into ArtifexSoftware:master Jan 7, 2024
dothinking added a commit that referenced this pull request Jan 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants