项目背景
在测试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_htmlreturn PyDocXHTMLExporter(path_or_stream).export()^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^File "D:\Python\CondaEnvs\DocumentProcessor\Lib\site-packages\pydocx\export\html.py", line 208, in exportreturn ''.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 exportself._first_pass_export()File "D:\Python\CondaEnvs\DocumentProcessor\Lib\site-packages\pydocx\export\base.py", line 129, in _first_pass_exportfor result in self.export_node(document):File "D:\Python\CondaEnvs\DocumentProcessor\Lib\site-packages\pydocx\export\base.py", line 218, in export_nodefor result in results:File "D:\Python\CondaEnvs\DocumentProcessor\Lib\site-packages\pydocx\export\html.py", line 127, in applyfor result in results:File "D:\Python\CondaEnvs\DocumentProcessor\Lib\site-packages\pydocx\export\base.py", line 218, in export_nodefor result in results:File "D:\Python\CondaEnvs\DocumentProcessor\Lib\site-packages\pydocx\export\html.py", line 127, in applyfor result in results:File "D:\Python\CondaEnvs\DocumentProcessor\Lib\site-packages\pydocx\export\base.py", line 252, in yield_nestedfor result in func(item):File "D:\Python\CondaEnvs\DocumentProcessor\Lib\site-packages\pydocx\export\base.py", line 218, in export_nodefor result in results:File "D:\Python\CondaEnvs\DocumentProcessor\Lib\site-packages\pydocx\export\html.py", line 278, in export_paragraphresults = 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_whitespacefor item in gen:File "D:\Python\CondaEnvs\DocumentProcessor\Lib\site-packages\pydocx\export\base.py", line 252, in yield_nestedfor result in func(item):File "D:\Python\CondaEnvs\DocumentProcessor\Lib\site-packages\pydocx\export\base.py", line 216, in export_noderesults = caller(node)^^^^^^^^^^^^File "D:\Python\CondaEnvs\DocumentProcessor\Lib\site-packages\pydocx\export\base.py", line 344, in export_runif 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):