Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I make pdf2image work with PDFs that have paths containing Chinese characters?

Following this question, I tried to run the following code to convert PDF with a path that contains Chinese characters to images:

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

I got this error message:

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.

in which all Chinese characters are replaced with "?".

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

The issue is caused solely by the Chinese characters in the directory since the program worked as intended after I ensured that the path contains no Chinese characters.

In pdf2image.py, I tried to alter the function pdfinfo_from_path, that out.decode("utf8", "ignore") is changed to e.g. out.decode("utf32", "ignore"), which also does not work.

Not sure whether it is relevant: according to the aforementioned answer, I also need to install poppler. But my code also worked when the directory does not contain any Chinese characters. In addition, running this code conda install -c conda-forge poppler (from the answer above) never ends after several hours of waiting.

>Solution :

You could use the convert_from_bytes to avoid the issue:

from pdf2image import convert_from_bytes

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

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading