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

Question:

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 "?".

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.

Asked By: Aqqqq

||

Answers:

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)
Answered By: Xiddoc
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.