21
loading...
This website collects cookies to deliver better user experience
.pdf
or .jpg/jpeg
format. I used online conversion sites to convert between the two formats. Recently a concern had been lingering in my mind that why am I uploading my personal documents on clouds and shouldn't I just create a converter so that the process stays local on my device. Hence, I decided to make a program to do the conversions.![]() |
![]() |
---|
conversion_mode
:conversion_mode == 'jpg'
, then set text accordingly and show entry field for inputting name of output PDF file.conversion_mode == 'pdf'
, then make relevant textual changes. No need to add entry field as the output JPGs will have the PDF's name followed by index.initialdir
to the directory where script is placed. curr_dir = os.getcwd()
filedialog.askopenfilename
is used.
filedialog.askopenfilename(initialdir=curr_dir, title="Select PDF",
filetypes=[("PDF File", "*.pdf")])
filedialog.askopenfilenames
is used.
filedialog.askopenfilenames(initialdir=curr_dir, title="Select JPG(s)",
filetypes=[("JPG File", "*.jpeg *.jpg")
file_paths
, which is a list of image file paths, to img2pdf's convert
function.import img2pdf
with open(name+".pdf", "wb+") as file:
file.write(img2pdf.convert(*file_paths))
name
is the output PDF name provided by the user.pdf2image
, firstly download poppler
on your system. (Instructions). Since I'm a Windows user, I downloaded poppler and placed the bin/
folder in the same directory as the script.poppler_path=r"./bin" if (sys.platform == "win32" or sys.platform == "cygwin") else None
output_folder
. To avoid out-of-memory error, I set paths_only=True
so that convert_from_path(...)
doesn't need to return PIL Image objects.from pdf2image import convert_from_path
from pdf2image.exceptions import (
PDFInfoNotInstalledError,
PDFPageCountError,
PDFSyntaxError
)
convert_from_path(
file_path,
output_folder=output_folder_path,
output_file=output_folder_name,
fmt="jpg",
paths_only=True,
poppler_path=r"./bin" if (sys.platform == "win32" or sys.platform == "cygwin") else None
)