Coding project for daily task automation utilizing Python and the pdf2image library.
I created this script to quickly convert PDF documents into PNG images, particularly for use on websites that only accept image uploads. This is especially useful for uploading certificates or diplomas originally saved as PDFs.
The script uses the pdf2image library to convert each page of a PDF into a high-quality PNG image and saves the output in a dedicated folder. It automatically creates the output directory if it doesn’t exist and generates clear, web-friendly image files ready for upload. This code was structured by combining my own research with support from ChatGPT.
Code
# Importing libraries
from pdf2image import convert_from_path
import os
# Variables
file_name = "Certificate"
# Base path
base_path = r"C:\\Users\\User\\OneDrive\\Desktop"
# Files paths
pdf_path = f"{base_path}\\{file_name}.pdf"
output_folder = f"{base_path}\\{file_name}"
# Convert PDF to PNG
def pdf_to_png(pdf_path, output_folder):
try:
images = convert_from_path(pdf_path)
for i, image in enumerate(images):
output_file = os.path.join(output_folder, f'{file_name}_{i + 1}.png')
image.save(output_file, 'PNG')
print(f"Saved: {output_file}")
print("PDF successfully converted to PNG!")
except Exception as e:
print(f"Error: {e}")
os.makedirs(output_folder, exist_ok=True)
pdf_to_png(pdf_path, output_folder)