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

<class 'UnicodeDecodeError'> returned a result with an error set #144

Closed
qq774724635 opened this issue Jun 24, 2022 · 12 comments
Closed

<class 'UnicodeDecodeError'> returned a result with an error set #144

qq774724635 opened this issue Jun 24, 2022 · 12 comments
Assignees
Labels
bug Something isn't working upstream

Comments

@qq774724635
Copy link

环境:python 3.7,pip 22.1.2,pdf2docx 0.5.4,PyMuPDF 1.20.0,python-docx 0.8.11

步骤代码:
image

报错情况:
image

@qq774724635
Copy link
Author

再上传一部分截图吧
image

@dothinking
Copy link
Contributor

这个报错截图看不出问题,请把报错记录中显示原始出错位置的部分截图看看。

@qq774724635
Copy link
Author

目前看来应该是pdf格式的问题,我的pdf格式为ASNI,转换成utf-8是可以的,但是转换为已经没有内容了

@qq774724635
Copy link
Author

image

@dothinking
Copy link
Contributor

问题的源头是倒数第四行:get_texttrace(),这是上游的一个处理PDF的库PyMuPDF的方法,已经超出我能控制的范围了。

https://pymupdf.readthedocs.io/en/latest/functions.html#Page.get_texttrace

@dothinking
Copy link
Contributor

目前看来应该是pdf格式的问题,我的pdf格式为ASNI,转换成utf-8是可以的,但是转换为已经没有内容了

pdf格式为ASNI怎么理解,你是如何转换成utf-8的?

@dothinking dothinking self-assigned this Jun 30, 2022
@ranger2001
Copy link

遇到类似问题,是一个msoffice2007创建的pdf文件。

@dothinking
Copy link
Contributor

遇到类似问题,是一个msoffice2007创建的pdf文件。

不知是否方便上传或者发我邮箱你的pdf以便查找原因?感谢。

@ddzzhen
Copy link

ddzzhen commented Jul 25, 2022

这个问题看起来与我的很相似,似乎是编码问题,无法执行get_texttrace函数
#155

@ranger2001
Copy link

遇到类似问题,是一个msoffice2007创建的pdf文件。

不知是否方便上传或者发我邮箱你的pdf以便查找原因?感谢。
刚看到,希望有帮助。
2.PDF

@dothinking dothinking removed the input needed Need test file label Jul 30, 2022
@dothinking
Copy link
Contributor

@ranger2001 感谢提供测试文件。

可以确定是 get_texttrace() 的问题,这是上游处理PDF的库 PyMuPDF 提供的方法,而它又是对上上游MuPDF相应函数的封装。彻底解决这个问题只有等他们官方修复,周期会比较长。

临时地,可以采用try-except的方式忽略这个错误。例如,找到 RawPageFitz.py 第70行(...\site-packages\pdf2docx\page\RawPageFitz.py):

try:
    spans = self.page_engine.get_texttrace()
except SystemError:
    # logging.warning('Ignore hidden text checking due to UnicodeDecodeError in upstream library.')
    spans = []

@dothinking
Copy link
Contributor

pdf2docxget_texttrace() 来检测隐藏的文本,然后根据需要是否输出到转化后的docx。例如,一些扫描的PDF书籍尤其是年代较远的文献,在扫描的图片层后面隐藏着一个OCR的文本层,方便文字复制和搜索。此时,可以通过设置参数ocr=0或者ocr=1来选择只输出图片或者只输出文本到docx(避免图片和文本的重叠),参考 #132

综合来看,绝大多数情况下都不需要考虑隐藏文本的问题,并且get_texttrace() 仅仅是对某些中文字体可能有问题,因此以上临时修复适用于绝大多数情况。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working upstream
Projects
None yet
Development

No branches or pull requests

4 participants