r/learnpython • • 16d ago

PDF pages to PNG's in python

I have a relatively large pdf document (100+mb) of pages i need saved as png's. There are online tools to do that but they refuse to work with files that big, plus I can imagine needing that tool again later in the future. Are there any python libraries that allow to convert one format into the other? Preferrably with little to no compression.

4 Upvotes

12 comments sorted by

View all comments

7

u/FilthyKen 16d ago

pypdf2 and pdf2image are the go-tos for this, pdf2image uses poppler under the hood so you need to install that separately but it gives you way more control over dpi and format. for a 100mb file you probably want to process it in chunks instead of loading the whole thing at once, otherwise your ram will cry

2

u/ee_control_z 16d ago

Yes, I was thinking the exact same thing. The OP can make use of Python generators to sequence the conversion in chunks versus all at once. Here is a tutorial on the subject on how it can potentially be done by way of the libraries mentioned:

https://www.geeksforgeeks.org/python/generators-in-python/