** PIP LIST LIST ALL INSTALLED PACKAGES **
npm list
npm cache purge
PYTHON
To run this script on your laptop, you need to install the required dependencies. Here's what each part of
the code does and what you need to install:
Step 1: Install Required Libraries
Open the command prompt (cmd) and run the following command:
pip install pymupdf tk
Explanation of Installed Libraries:
pymupdf (fitz) → This library allows reading, editing, and splitting PDFs.
tk (Tkinter) → This is a built-in Python library used to create graphical interfaces, such as file upload
dialogs.
Step 2: Explanation of the Code
1. Import Required Libraries
python
Copy
Edit
import fitz # PyMuPDF (for handling PDFs)
import tkinter as tk # Tkinter (for GUI)
from tkinter import filedialog # File dialog for selecting the PDF
fitz (from PyMuPDF) is used to open, read, and manipulate PDF files.
tkinter is used to create a graphical user interface (GUI) to select files.
2. Function to Split the PDF
python
Copy
Edit
def split_pdf(input_pdf, pages_per_split):
doc = fitz.open(input_pdf) # Open the PDF file
total_pages = len(doc) # Get the total number of pages
for i in range(0, total_pages, pages_per_split): # Loop through the document in chunks
output_pdf = f"split_{i+1}-{i+pages_per_split}.pdf" # Name for each split file
new_doc = fitz.open() # Create a new PDF document
new_doc.insert_pdf(doc, from_page=i, to_page=min(i+pages_per_split-1, total_pages-1)) # Extract
and insert pages
new_doc.save(output_pdf) # Save the split PDF
new_doc.close() # Close the file
print(f"Saved: {output_pdf}") # Print confirmation
Opens the selected PDF file.
Splits it into smaller PDFs based on the specified number of pages.
Saves each split PDF with a different name.
3. Function to Open File Dialog and Select a PDF
python
Copy
Edit
def upload_file():
root = tk.Tk()
root.withdraw() # Hide the main Tkinter window
file_path = filedialog.askopenfilename(filetypes=[("PDF Files", "*.pdf")]) # Open file dialog
if file_path:
split_pdf(file_path, 10) # Call split function with 10 pages per split
Opens a file dialog to let the user choose a PDF file.
Passes the selected file to the split_pdf function with 10 pages per split.
4. Main Execution
python
Copy
Edit
if __name__ == "__main__":
upload_file()
Runs the script and opens the file selection dialog.
Final Steps
Save the script as split_pdf.py.
Run it using:
sh
Copy
Edit
python split_pdf.py
A file dialog will open—select a PDF.
The script will split the PDF into smaller files and save them in the same directory.
Let me know if you have any questions! 🚀