Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from PyPDF2 import PdfWriter, PdfReader
|
| 4 |
+
|
| 5 |
+
def split_pdf(input_file, pages_per_split):
|
| 6 |
+
# Open the input PDF file
|
| 7 |
+
with open(input_file.name, 'rb') as file:
|
| 8 |
+
# Create a PDF reader object
|
| 9 |
+
pdf_reader = PdfReader(file)
|
| 10 |
+
|
| 11 |
+
# Get the total number of pages in the PDF
|
| 12 |
+
total_pages = len(pdf_reader.pages)
|
| 13 |
+
|
| 14 |
+
# Calculate the number of splits
|
| 15 |
+
num_splits = (total_pages + pages_per_split - 1) // pages_per_split
|
| 16 |
+
|
| 17 |
+
# Create a directory to store the split PDF files
|
| 18 |
+
output_dir = 'split_pdfs'
|
| 19 |
+
os.makedirs(output_dir, exist_ok=True)
|
| 20 |
+
|
| 21 |
+
# List to store the paths of the split PDF files
|
| 22 |
+
split_files = []
|
| 23 |
+
|
| 24 |
+
# Iterate over the number of splits
|
| 25 |
+
for i in range(num_splits):
|
| 26 |
+
# Create a new PDF writer object for each split
|
| 27 |
+
pdf_writer = PdfWriter()
|
| 28 |
+
|
| 29 |
+
# Calculate the start and end page numbers for the current split
|
| 30 |
+
start_page = i * pages_per_split
|
| 31 |
+
end_page = min((i + 1) * pages_per_split, total_pages)
|
| 32 |
+
|
| 33 |
+
# Add the pages to the PDF writer object
|
| 34 |
+
for page in range(start_page, end_page):
|
| 35 |
+
pdf_writer.add_page(pdf_reader.pages[page])
|
| 36 |
+
|
| 37 |
+
# Generate the output file name for the current split
|
| 38 |
+
output_file = os.path.join(output_dir, f'split_{i+1}.pdf')
|
| 39 |
+
|
| 40 |
+
# Open the output file in write-binary mode
|
| 41 |
+
with open(output_file, 'wb') as output:
|
| 42 |
+
# Write the pages to the output file
|
| 43 |
+
pdf_writer.write(output)
|
| 44 |
+
|
| 45 |
+
# Add the path of the split PDF file to the list
|
| 46 |
+
split_files.append(output_file)
|
| 47 |
+
|
| 48 |
+
return split_files
|
| 49 |
+
|
| 50 |
+
# Gradio interface
|
| 51 |
+
def gradio_interface(input_file, pages_per_split):
|
| 52 |
+
split_files = split_pdf(input_file, pages_per_split)
|
| 53 |
+
return split_files
|
| 54 |
+
|
| 55 |
+
# Create a Gradio interface
|
| 56 |
+
iface = gr.Interface(
|
| 57 |
+
fn=gradio_interface,
|
| 58 |
+
inputs=[gr.File(label="Input PDF"), gr.Number(label="Pages per Split")],
|
| 59 |
+
outputs=gr.File(label="Split PDFs"),
|
| 60 |
+
title="PDF Splitter",
|
| 61 |
+
description="Split a large PDF file into smaller PDF files.",
|
| 62 |
+
)
|
| 63 |
+
|
| 64 |
+
# Launch the Gradio interface
|
| 65 |
+
iface.launch()
|