gpt4 book ai didi

python - 如何使 pdf2image 与路径包含中文字符的 PDF 一起工作?

转载 作者:行者123 更新时间:2023-12-05 04:19:11 40 4
gpt4 key购买 nike

正在关注 this question ,我尝试运行以下代码将路径包含汉字的PDF转换为图像:

from pdf2image import convert_from_path
images = convert_from_path('path with Chinese character in it/some Chinese character.pdf', 500)
# save images

我收到此错误消息:

PDFPageCountError: Unable to get page count.
I/O Error: Couldn't open file 'path with Chinese character in it/??????.pdf': No such file or directory.

其中所有的汉字都用“?”代替。

问题完全是由目录中的中文字符引起的,因为在我确保路径不包含中文字符后程序按预期运行。

pdf2image.py 中,我试图改变函数 pdfinfo_from_path,即 out.decode("utf8", "ignore")改为例如out.decode("utf32", "ignore"),同样不起作用。

不确定是否相关:根据上述回答,我还需要安装poppler。但是当目录不包含任何汉字时,我的代码也能正常工作。此外,运行此代码 conda install -c conda-forge poppler(来自上面的答案)在等待几个小时后永远不会结束。

最佳答案

您可以使用 convert_from_bytes 来避免这个问题:

from pdf2image import convert_from_bytes

with open('chinese_filename.pdf', 'rb') as f:
images = convert_from_bytes(f.read(), 500)

关于python - 如何使 pdf2image 与路径包含中文字符的 PDF 一起工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74913169/

40 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com