【问题标题】:How can I make pdf2image work with PDFs that have paths containing Chinese characters?如何使 pdf2image 与路径包含中文字符的 PDF 一起工作?
【发布时间】:2022-12-25 20:19:25
【问题描述】:

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(来自上面的答案)在等待几个小时后永远不会结束。

【问题讨论】:

    标签: python image pdf


    【解决方案1】:

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

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

    【讨论】:

    • 哇,太快了 :o
    • @kosciej16 必须帮助人们完成工作 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-12
    • 2019-04-28
    • 2014-09-23
    • 2016-01-01
    • 2018-02-24
    • 2011-02-10
    • 2021-10-15
    相关资源
    最近更新 更多