Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,11 +1,10 @@
|
|
| 1 |
import os
|
| 2 |
from fpdf import FPDF
|
| 3 |
from PyPDF2 import PdfMerger
|
| 4 |
-
from docx import Document
|
| 5 |
from PIL import Image
|
| 6 |
import gradio as gr
|
| 7 |
-
import aspose.words as aw
|
| 8 |
-
|
| 9 |
|
| 10 |
# Function to add a single text input as a new page in an FPDF object
|
| 11 |
def add_text_as_page(pdf, text):
|
|
@@ -31,18 +30,20 @@ def merge_pdfs(pdfs):
|
|
| 31 |
merger.close()
|
| 32 |
return output_path
|
| 33 |
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
doc.
|
|
|
|
| 37 |
|
| 38 |
# Main function to process uploaded files and manual text inputs
|
| 39 |
-
def process_files_and_text(files, manual_texts):
|
| 40 |
pdfs = []
|
| 41 |
manual_text_pdf = "manual_texts.pdf"
|
| 42 |
pdf = FPDF()
|
| 43 |
|
| 44 |
-
# Process uploaded files
|
| 45 |
-
for
|
|
|
|
| 46 |
file_name, file_extension = os.path.splitext(file.name)
|
| 47 |
|
| 48 |
if file_extension.lower() == ".pdf":
|
|
@@ -53,8 +54,11 @@ def process_files_and_text(files, manual_texts):
|
|
| 53 |
add_text_as_page(pdf, text)
|
| 54 |
|
| 55 |
elif file_extension.lower() == ".docx":
|
| 56 |
-
|
|
|
|
|
|
|
| 57 |
pdfs.append(file_name + ".pdf")
|
|
|
|
| 58 |
elif file_extension.lower() in [".png", ".jpg", ".jpeg"]:
|
| 59 |
pdf_output = image_to_pdf(file.name)
|
| 60 |
pdfs.append(pdf_output)
|
|
@@ -73,9 +77,17 @@ def process_files_and_text(files, manual_texts):
|
|
| 73 |
merged_pdf = merge_pdfs(pdfs)
|
| 74 |
return merged_pdf
|
| 75 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
# Gradio interface function
|
| 77 |
-
def file_merger(files, texts):
|
| 78 |
-
merged_pdf = process_files_and_text(files, texts)
|
| 79 |
return merged_pdf
|
| 80 |
|
| 81 |
# Define Gradio interface
|
|
@@ -92,6 +104,17 @@ with gr.Blocks() as iface:
|
|
| 92 |
interactive=True
|
| 93 |
)
|
| 94 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
add_more_button = gr.Button("Add More Text")
|
| 96 |
merge_button = gr.Button("Merge Files and Text")
|
| 97 |
output = gr.File(label="Download Merged PDF")
|
|
@@ -103,10 +126,27 @@ with gr.Blocks() as iface:
|
|
| 103 |
state_texts.append(current_text)
|
| 104 |
return "", state_texts # Clear the input box and update the state
|
| 105 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
# Add functionality to add more text
|
| 107 |
add_more_button.click(add_text_to_state, inputs=[manual_text_inputs, manual_texts_state], outputs=[manual_text_inputs, manual_texts_state])
|
| 108 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
# Merge files and text
|
| 110 |
-
merge_button.click(file_merger, inputs=[file_input, manual_texts_state], outputs=output)
|
| 111 |
|
| 112 |
-
iface.launch()
|
|
|
|
| 1 |
import os
|
| 2 |
from fpdf import FPDF
|
| 3 |
from PyPDF2 import PdfMerger
|
|
|
|
| 4 |
from PIL import Image
|
| 5 |
import gradio as gr
|
| 6 |
+
#import aspose.words as aw
|
| 7 |
+
import pypandoc
|
| 8 |
|
| 9 |
# Function to add a single text input as a new page in an FPDF object
|
| 10 |
def add_text_as_page(pdf, text):
|
|
|
|
| 30 |
merger.close()
|
| 31 |
return output_path
|
| 32 |
|
| 33 |
+
# Convert DOCX to PDF
|
| 34 |
+
#def docx_to_pdf(docx_file, pdf_file):
|
| 35 |
+
# doc = aw.Document(docx_file)
|
| 36 |
+
# doc.save(pdf_file)
|
| 37 |
|
| 38 |
# Main function to process uploaded files and manual text inputs
|
| 39 |
+
def process_files_and_text(files, manual_texts, file_order):
|
| 40 |
pdfs = []
|
| 41 |
manual_text_pdf = "manual_texts.pdf"
|
| 42 |
pdf = FPDF()
|
| 43 |
|
| 44 |
+
# Process uploaded files according to file_order
|
| 45 |
+
for file_name in file_order:
|
| 46 |
+
file = next(f for f in files if f.name == file_name)
|
| 47 |
file_name, file_extension = os.path.splitext(file.name)
|
| 48 |
|
| 49 |
if file_extension.lower() == ".pdf":
|
|
|
|
| 54 |
add_text_as_page(pdf, text)
|
| 55 |
|
| 56 |
elif file_extension.lower() == ".docx":
|
| 57 |
+
pypandoc.convert_file(file.name, 'pdf', outputfile=file_name + ".pdf")
|
| 58 |
+
#pdf_file = file_name + ".pdf"
|
| 59 |
+
#docx_to_pdf(file.name, pdf_file)
|
| 60 |
pdfs.append(file_name + ".pdf")
|
| 61 |
+
|
| 62 |
elif file_extension.lower() in [".png", ".jpg", ".jpeg"]:
|
| 63 |
pdf_output = image_to_pdf(file.name)
|
| 64 |
pdfs.append(pdf_output)
|
|
|
|
| 77 |
merged_pdf = merge_pdfs(pdfs)
|
| 78 |
return merged_pdf
|
| 79 |
|
| 80 |
+
# Function to move items up or down in the list
|
| 81 |
+
def reorder_files(file_list, direction, index):
|
| 82 |
+
if direction == "up" and index > 0:
|
| 83 |
+
file_list[index], file_list[index - 1] = file_list[index - 1], file_list[index]
|
| 84 |
+
elif direction == "down" and index < len(file_list) - 1:
|
| 85 |
+
file_list[index], file_list[index + 1] = file_list[index + 1], file_list[index]
|
| 86 |
+
return file_list
|
| 87 |
+
|
| 88 |
# Gradio interface function
|
| 89 |
+
def file_merger(files, texts, file_order):
|
| 90 |
+
merged_pdf = process_files_and_text(files, texts, file_order)
|
| 91 |
return merged_pdf
|
| 92 |
|
| 93 |
# Define Gradio interface
|
|
|
|
| 104 |
interactive=True
|
| 105 |
)
|
| 106 |
|
| 107 |
+
file_list_state = gr.State([])
|
| 108 |
+
file_display = gr.Textbox(
|
| 109 |
+
label="Uploaded Files (Reorder Below)",
|
| 110 |
+
interactive=False,
|
| 111 |
+
lines=10
|
| 112 |
+
)
|
| 113 |
+
file_index = gr.Number(label="File Index to Move (Starting from 0)", value=0, interactive=True)
|
| 114 |
+
with gr.Row():
|
| 115 |
+
move_up_button = gr.Button("Move Up")
|
| 116 |
+
move_down_button = gr.Button("Move Down")
|
| 117 |
+
|
| 118 |
add_more_button = gr.Button("Add More Text")
|
| 119 |
merge_button = gr.Button("Merge Files and Text")
|
| 120 |
output = gr.File(label="Download Merged PDF")
|
|
|
|
| 126 |
state_texts.append(current_text)
|
| 127 |
return "", state_texts # Clear the input box and update the state
|
| 128 |
|
| 129 |
+
def update_file_list(files):
|
| 130 |
+
file_names = [file.name for file in files]
|
| 131 |
+
return file_names, "\n".join(file_names)
|
| 132 |
+
|
| 133 |
+
def handle_reorder(file_list, direction, index):
|
| 134 |
+
if 0 <= index < len(file_list):
|
| 135 |
+
reordered_list = reorder_files(file_list, direction, index)
|
| 136 |
+
return reordered_list, "\n".join(reordered_list)
|
| 137 |
+
return file_list, "\n".join(file_list) # No change if index is out of range
|
| 138 |
+
|
| 139 |
# Add functionality to add more text
|
| 140 |
add_more_button.click(add_text_to_state, inputs=[manual_text_inputs, manual_texts_state], outputs=[manual_text_inputs, manual_texts_state])
|
| 141 |
+
|
| 142 |
+
# Update file list when files are uploaded
|
| 143 |
+
file_input.change(update_file_list, inputs=[file_input], outputs=[file_list_state, file_display])
|
| 144 |
+
|
| 145 |
+
# Move files up or down
|
| 146 |
+
move_up_button.click(handle_reorder, inputs=[file_list_state, gr.Text("up"), file_index], outputs=[file_list_state, file_display])
|
| 147 |
+
move_down_button.click(handle_reorder, inputs=[file_list_state, gr.Text("down"), file_index], outputs=[file_list_state, file_display])
|
| 148 |
+
|
| 149 |
# Merge files and text
|
| 150 |
+
merge_button.click(file_merger, inputs=[file_input, manual_texts_state, file_list_state], outputs=output)
|
| 151 |
|
| 152 |
+
iface.launch(share=True)
|