Coding project for daily task automation utilizing Python and Ghostscript.
This Python script was structured by combining my own research with support from ChatGPT. It uses Ghostscript to compress my portfolio PDF, allowing me to easily switch between different output qualities (72, 150 or 300 DPI). This makes it simple to adapt the file size for various platforms with upload limits.
Code
# Importing libraries
import subprocess
# Variables
file_name = "Portfolio"
quality = "/printer" # /printer = 300 DPI, /ebook = 150 DPI, /screen = 72 DPI
# Base path
base_path = f"G:\\Meu Drive\\04_Professional\\02_CV\\06_2025\\{file_name}"
# File paths
input_path = f"{base_path}\\{file_name}_original.pdf"
output_path = f"{base_path}\\{file_name}.pdf"
gs_path = r'C:\Program Files\gs\gs10.05.0\bin\gswin64c.exe'
# Compress PDF
def compress_pdf(input_file, output_file):
gs_command = [
gs_path,
"-sDEVICE=pdfwrite",
"-dCompatibilityLevel=1.4",
f"-dPDFSETTINGS={quality}",
"-dNOPAUSE",
"-dQUIET",
"-dBATCH",
f"-sOutputFile={output_file}",
input_file
]
subprocess.run(gs_command, check=True)
print("PDF compressed successfully.")
compress_pdf(input_path, output_path)