I want to create an exe that can be deployed onto other computers. The program needs to be able to read pdf's and turn them into images, but I don't want other users to have to download dependencies.
My understanding is that py2image and wand both require external dependencies that, if you convert to a exe, other users would also need to download the dependencies themselves.
Are there other options available/ workarounds ?
Actually, it took me a while to handle this, but I think it worth it. You need to do all steps carefully to make it work.
pip install pdf2image
.myproject
.converter.py
inside myproject
and add below code.myproject
and name it poppler
.poppler
directory. Try to test pdfimages.exe
if it is working.pyinstaller converter.py -F --add-data "./poppler/*;./poppler" --noupx
converter.exe myfile.pdf
. Results would be created inside the output
directory next to the executable.converter.py
:
import sys
import os
from pdf2image import convert_from_path
def current_path(dir_path):
if hasattr(sys, '_MEIPASS'):
return os.path.join(sys._MEIPASS, dir_path)
return os.path.join(".", dir_path)
if __name__ == "__main__":
if len(sys.argv) < 2:
print("PASS your PDF file: \"converter.exe myfile.pdf\"")
input()
sys.exit(0)
os.environ["PATH"] += os.pathsep + \
os.pathsep.join([current_path("poppler")])
if not os.path.isdir("./output"):
os.makedirs("output")
images = convert_from_path(sys.argv[-1], 500)
for image, i in zip(images, (range(len(images)))):
image.save('./output/out{}.png'.format(i), 'PNG')
PS: If you like it, you can add a GUI and add more settings for pdf2images
.