import os
import sys
import tkinter as tk
from tkinter import filedialog, messagebox
# Check if library is installed
try:
from ilovepdf import ILovePdf
except ImportError:
print("Error: Library not found.")
print("Please run this command in your terminal: pip install ilovepdf")
input("Press Enter to exit...")
sys.exit()
def convert_pdf_to_excel():
# ---------------------------------------------------------
# CONFIGURATION
# ---------------------------------------------------------
# Your provided Public Key
PUBLIC_KEY = 'roarhassan8@gmail.com_9h0xBNeQBnpC21VCGRxfGq7Xt065H1dMVPLPl9MTphiApGrDLPY8DVnonKoSSx9i'
# ⚠️ YOU MUST PASTE YOUR SECRET KEY BELOW ⚠️
# Go to developer.ilovepdf.com -> My Projects to find it.
SECRET_KEY = 'PASTE_YOUR_SECRET_KEY_HERE'
# ---------------------------------------------------------
if SECRET_KEY == 'PASTE_YOUR_SECRET_KEY_HERE':
messagebox.showerror("Error", "Please open the code and paste your Secret Key where indicated.")
return
# 1. Select File
root = tk.Tk()
root.withdraw() # Hide the main window
file_path = filedialog.askopenfilename(
title="Select PDF to Convert",
filetypes=[("PDF Files", "*.pdf")]
)
if not file_path:
return # User cancelled
try:
print(f"Processing: {file_path}...")
# 2. Initialize API
ilove = ILovePdf(PUBLIC_KEY, SECRET_KEY)
# 3. Create Task (pdfexcel is the tool name for PDF to Excel)
task = ilove.new_task('pdfexcel')
# 4. Upload File
print("Uploading file...")
task.add_file(file_path)
# 5. Process
print("Converting...")
task.process()
# 6. Download
output_folder = os.path.dirname(file_path)
print("Downloading result...")
task.download(output_folder)
messagebox.showinfo("Success", f"Conversion Complete!\nSaved in: {output_folder}")
print("Done.")
except Exception as e:
messagebox.showerror("Error", f"An error occurred:\n{str(e)}")
print(f"Error: {e}")
if __name__ == "__main__":
convert_pdf_to_excel()