Split pdf
Table of contents
Here is a Python script to split your 300-page PDF into 10 equal parts (30 pages each) using PyPDF2:
#!/usr/bin/env python3
from PyPDF2 import PdfReader, PdfWriter
# Define input and output file paths
input_pdf_path = "/media/backup_006_3/workspace.01.main/books_ws/business.analyst/Mankiw.PrinciplesOfEconomicsHQ.pdf"
output_folder = "/media/backup_006_3/workspace.01.main/books_ws/business.analyst/"
# Open the input PDF
reader = PdfReader(input_pdf_path)
total_pages = len(reader.pages)
# Define number of parts
num_parts = 10
pages_per_part = total_pages // num_parts # 300 pages / 10 parts = 30 pages per part
# Split into 10 parts
for i in range(num_parts):
writer = PdfWriter()
start_page = i * pages_per_part
end_page = start_page + pages_per_part
for page in range(start_page, end_page):
writer.add_page(reader.pages[page])
output_pdf_path = f"{output_folder}Mankiw_Part{i+1}.pdf"
with open(output_pdf_path, "wb") as output_pdf:
writer.write(output_pdf)
print(f"Created: {output_pdf_path}")
print("Splitting complete!")
How to Use
Install PyPDF2 if you haven't already:
pip install PyPDF2
Save this script as
split_
pdf.py
.Run it:
python3 split_pdf.py
๐น Output:
This will create 10 PDF files, named:
Mankiw_Part1.pdf
(Pages 1-30)Mankiw_Part2.pdf
(Pages 31-60)โฆ
Mankiw_Part10.pdf
(Pages 271-300)
๐ Let me know if you need modifications!
ย