Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import fitz # PyMuPDF
- def split_pdf(input_pdf, ranges_and_names):
- # Open the input PDF
- pdf = fitz.open(input_pdf)
- for range_and_name in ranges_and_names:
- range_and_name_parts = range_and_name.split(' ')
- start_page, end_page = map(int, range_and_name_parts[0].split('-'))
- output_pdf = f"{range_and_name_parts[1]}.pdf" # Extract the name from the range
- # Create a new PDF writer
- writer = fitz.open()
- # Add pages to the new PDF
- for page_num in range(start_page - 1, end_page):
- writer.insert_pdf(pdf, from_page=page_num, to_page=page_num)
- # Save the new PDF
- writer.save(output_pdf)
- print(f"PDF created: {output_pdf}")
- # Page ranges and corresponding names
- ranges_and_names = [
- "1-15 1_GENERAL_SURGERY_REVISION",
- "16-25 2_GENERAL_SURGERY_2_&_SHOCK",
- "26-35 3_TRAUMA",
- "36-47 4_TRAUMA_2_AND_BURNS",
- "48-61 5_BREAST_DISORDERS",
- "62-73 6_THYROID_AND_PARATHYROID",
- "74-85 7_GIT_1",
- "86-93 8_GIT_2",
- "94-110 9_GIT_3",
- "111-126 10_GIT_4",
- "127-139 11_HEPATOBILIARY",
- "140-152 12_HEPATOBILIARY_2_&_PANCREAS",
- "153-169 13_VASCULAR_SURGERY",
- "170-188 14_HERNIA_THORAX_AND_SKIN",
- "189-204 15_UROLOGY_1",
- "205-217 16_UROLOGY_2",
- "218-233 17_TRANSPLANT_PLASTIC_SURGERY_AND_ADRENAL",
- "234-243 18_ORAL_CANCER_&_SALIVARY_GLANDS"
- ]
- # Input PDF
- input_pdf = 'name.pdf'
- # Call the function to split the PDF
- split_pdf(input_pdf, ranges_and_names)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement