Spaces:
Build error
Build error
File size: 1,510 Bytes
f5ba12a 191aec7 f5ba12a |
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 |
import gradio as gr
import PyPDF2
import os
def merge_pdfs(pdf_list, output_filename):
"""
Merge multiple PDF files into a single PDF file with a specified filename.
Parameters:
pdf_list (list): List of uploaded PDF files (file-like objects).
output_filename (str): Desired name of the output merged PDF file.
Returns:
str: Path to the merged PDF file.
"""
pdf_merger = PyPDF2.PdfMerger()
# If no extension is provided, add .pdf
if not output_filename.lower().endswith('.pdf'):
output_filename += '.pdf'
for pdf_file in pdf_list:
pdf_file_path = pdf_file.name # Get the path from the uploaded file object
pdf_merger.append(pdf_file_path)
with open(output_filename, 'wb') as output_pdf:
pdf_merger.write(output_pdf)
return output_filename
# Gradio interface
def gradio_pdf_merge(pdf_files, output_filename):
output_pdf_path = merge_pdfs(pdf_files, output_filename)
return output_pdf_path
# Create the Gradio interface
gr_interface = gr.Interface(
fn=gradio_pdf_merge, # The function to run when files are uploaded
inputs=[
gr.Files(file_types=['.pdf'], label="Upload PDF files"), # Allow multiple PDFs to be uploaded
gr.Textbox(label="Output PDF filename", placeholder="Enter the output filename (e.g., merged.pdf)") # Input for output filename
],
outputs=gr.File(label="Merged PDF") # Return the merged PDF file
)
# Launch the interface
gr_interface.launch()
|