File size: 2,196 Bytes
b11e20a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import os
import gradio as gr
from PyPDF2 import PdfWriter, PdfReader

def split_pdf(input_file, pages_per_split):
    # Open the input PDF file
    with open(input_file.name, 'rb') as file:
        # Create a PDF reader object
        pdf_reader = PdfReader(file)

        # Get the total number of pages in the PDF
        total_pages = len(pdf_reader.pages)

        # Calculate the number of splits
        num_splits = (total_pages + pages_per_split - 1) // pages_per_split

        # Create a directory to store the split PDF files
        output_dir = 'split_pdfs'
        os.makedirs(output_dir, exist_ok=True)

        # List to store the paths of the split PDF files
        split_files = []

        # Iterate over the number of splits
        for i in range(num_splits):
            # Create a new PDF writer object for each split
            pdf_writer = PdfWriter()

            # Calculate the start and end page numbers for the current split
            start_page = i * pages_per_split
            end_page = min((i + 1) * pages_per_split, total_pages)

            # Add the pages to the PDF writer object
            for page in range(start_page, end_page):
                pdf_writer.add_page(pdf_reader.pages[page])

            # Generate the output file name for the current split
            output_file = os.path.join(output_dir, f'split_{i+1}.pdf')

            # Open the output file in write-binary mode
            with open(output_file, 'wb') as output:
                # Write the pages to the output file
                pdf_writer.write(output)

            # Add the path of the split PDF file to the list
            split_files.append(output_file)

        return split_files

# Gradio interface
def gradio_interface(input_file, pages_per_split):
    split_files = split_pdf(input_file, pages_per_split)
    return split_files

# Create a Gradio interface
iface = gr.Interface(
    fn=gradio_interface,
    inputs=[gr.File(label="Input PDF"), gr.Number(label="Pages per Split")],
    outputs=gr.File(label="Split PDFs"),
    title="PDF Splitter",
    description="Split a large PDF file into smaller PDF files.",
)

# Launch the Gradio interface
iface.launch()