项目背景
在测试PyDocx代码时
```python from pydocx import PyDocX
html = PyDocX.to_html("test.docx") with open("test.html", 'w', encoding="utf-8") as f: f.write(html) ```
发生错误:
Traceback (most recent call last):
File "D:\Python\Projects\StudyProjecs\DocumentConverter\main.py", line 5, in <module>
html = PyDocX.to_html("test.docx")
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\Python\CondaEnvs\DocumentProcessor\Lib\site-packages\pydocx\pydocx.py", line 13, in to_html
return PyDocXHTMLExporter(path_or_stream).export()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\Python\CondaEnvs\DocumentProcessor\Lib\site-packages\pydocx\export\html.py", line 208, in export
return ''.join(
^^^^^^^^
File "D:\Python\CondaEnvs\DocumentProcessor\Lib\site-packages\pydocx\export\html.py", line 208, in <genexpr>
return ''.join(
^
File "D:\Python\CondaEnvs\DocumentProcessor\Lib\site-packages\pydocx\export\base.py", line 117, in export
self._first_pass_export()
File "D:\Python\CondaEnvs\DocumentProcessor\Lib\site-packages\pydocx\export\base.py", line 129, in _first_pass_export
for result in self.export_node(document):
File "D:\Python\CondaEnvs\DocumentProcessor\Lib\site-packages\pydocx\export\base.py", line 218, in export_node
for result in results:
File "D:\Python\CondaEnvs\DocumentProcessor\Lib\site-packages\pydocx\export\html.py", line 127, in apply
for result in results:
File "D:\Python\CondaEnvs\DocumentProcessor\Lib\site-packages\pydocx\export\base.py", line 218, in export_node
for result in results:
File "D:\Python\CondaEnvs\DocumentProcessor\Lib\site-packages\pydocx\export\html.py", line 127, in apply
for result in results:
File "D:\Python\CondaEnvs\DocumentProcessor\Lib\site-packages\pydocx\export\base.py", line 252, in yield_nested
for result in func(item):
File "D:\Python\CondaEnvs\DocumentProcessor\Lib\site-packages\pydocx\export\base.py", line 218, in export_node
for result in results:
File "D:\Python\CondaEnvs\DocumentProcessor\Lib\site-packages\pydocx\export\html.py", line 278, in export_paragraph
results = is_not_empty_and_not_only_whitespace(results)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\Python\CondaEnvs\DocumentProcessor\Lib\site-packages\pydocx\export\html.py", line 78, in is_not_empty_and_not_only_whitespace
for item in gen:
File "D:\Python\CondaEnvs\DocumentProcessor\Lib\site-packages\pydocx\export\base.py", line 252, in yield_nested
for result in func(item):
File "D:\Python\CondaEnvs\DocumentProcessor\Lib\site-packages\pydocx\export\base.py", line 216, in export_node
results = caller(node)
^^^^^^^^^^^^
File "D:\Python\CondaEnvs\DocumentProcessor\Lib\site-packages\pydocx\export\base.py", line 344, in export_run
if run.effective_properties:
^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\Python\CondaEnvs\DocumentProcessor\Lib\site-packages\pydocx\util\memoize.py", line 24, in __call__
if not isinstance(args, collections.Hashable):
^^^^^^^^^^^^^^^^^^^^
AttributeError: module 'collections' has no attribute 'Hashable'
原因
在 Python 3.3 之前的版本中,collections.Hashable
是存在的,但在 Python 3.3 及以后的版本中,它被移动到了 collections.abc
模块中。
解决方法
到路径
虚拟环境路径\Lib\site-packages\pydocx\util\memoize.py
到第24行左右
python
def __call__(self, *args):
if not isinstance(args, collections.Hashable):
# uncacheable. a list, for instance.
# better to not cache than blow up.
return self.func(*args)
if args in self.cache:
return self.cache[args]
else:
value = self.func(*args)
self.set_cache(value, *args)
return value
将
if not isinstance(args, collections.Hashable):
改为
if not isinstance(args, collections.abc.Hashable):