Spaces:
Sleeping
Sleeping
fix docx error
Browse files
app.py
CHANGED
|
@@ -1,80 +1,91 @@
|
|
| 1 |
-
from PyPDF2 import PdfWriter
|
| 2 |
-
from PIL import Image
|
| 3 |
-
from docx import Document
|
| 4 |
-
from io import BytesIO
|
| 5 |
import os
|
| 6 |
from fpdf import FPDF
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
self.set_font("DejaVu", size=12)
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
""
|
| 25 |
-
|
| 26 |
-
|
| 27 |
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
pdf_bytes.seek(0)
|
| 37 |
-
return pdf_bytes
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
elif ext in ['jpg', 'jpeg', 'png']:
|
| 49 |
-
# Convert image to PDF
|
| 50 |
-
img = Image.open(file)
|
| 51 |
-
pdf_bytes = BytesIO()
|
| 52 |
-
img.convert('RGB').save(pdf_bytes, format='PDF')
|
| 53 |
-
pdf_bytes.seek(0)
|
| 54 |
-
writer.append(pdf_bytes)
|
| 55 |
-
elif ext == 'docx':
|
| 56 |
-
# Convert DOCX to PDF-like content
|
| 57 |
-
pdf_bytes = docx_to_pdf(file)
|
| 58 |
-
writer.append(pdf_bytes)
|
| 59 |
-
elif ext == 'txt':
|
| 60 |
-
# Convert TXT file to PDF-like content
|
| 61 |
-
pdf_bytes = BytesIO()
|
| 62 |
-
pdf_bytes.write(file.read())
|
| 63 |
-
pdf_bytes.seek(0)
|
| 64 |
-
writer.append(pdf_bytes)
|
| 65 |
|
| 66 |
-
#
|
| 67 |
-
|
| 68 |
-
|
| 69 |
|
| 70 |
-
|
|
|
|
| 71 |
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
if uploaded_files:
|
| 78 |
-
output_pdf = merge_files(uploaded_files, "merged.pdf")
|
| 79 |
-
with open(output_pdf, "rb") as file:
|
| 80 |
-
st.download_button(label="Download Merged PDF", data=file, file_name="merged.pdf")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 io
|
| 7 |
|
| 8 |
+
# Function to convert DOCX file to text
|
| 9 |
+
def docx_to_text(docx_file):
|
| 10 |
+
doc = Document(docx_file)
|
| 11 |
+
text = ""
|
| 12 |
+
for para in doc.paragraphs:
|
| 13 |
+
text += para.text + "\n"
|
| 14 |
+
return text
|
|
|
|
| 15 |
|
| 16 |
+
# Function to add text as PDF
|
| 17 |
+
def add_text_as_pdf(pdf, text):
|
| 18 |
+
pdf.add_page()
|
| 19 |
+
pdf.set_auto_page_break(auto=True, margin=15)
|
| 20 |
+
pdf.set_font("Arial", size=12)
|
| 21 |
+
pdf.multi_cell(0, 10, text)
|
| 22 |
|
| 23 |
+
# Function to convert image to PDF
|
| 24 |
+
def image_to_pdf(image_file):
|
| 25 |
+
img = Image.open(image_file)
|
| 26 |
+
pdf_path = image_file + ".pdf"
|
| 27 |
+
img.convert('RGB').save(pdf_path)
|
| 28 |
+
return pdf_path
|
| 29 |
|
| 30 |
+
# Function to merge PDFs
|
| 31 |
+
def merge_pdfs(pdfs):
|
| 32 |
+
merger = PdfMerger()
|
| 33 |
+
for pdf in pdfs:
|
| 34 |
+
merger.append(pdf)
|
| 35 |
+
output_path = "merged_output.pdf"
|
| 36 |
+
merger.write(output_path)
|
| 37 |
+
merger.close()
|
| 38 |
+
return output_path
|
| 39 |
|
| 40 |
+
# Main function to process uploaded files
|
| 41 |
+
def process_files(files):
|
| 42 |
+
pdfs = []
|
|
|
|
|
|
|
| 43 |
|
| 44 |
+
for file in files:
|
| 45 |
+
file_name, file_extension = os.path.splitext(file.name)
|
| 46 |
+
|
| 47 |
+
if file_extension.lower() == ".pdf":
|
| 48 |
+
pdfs.append(file)
|
| 49 |
+
|
| 50 |
+
elif file_extension.lower() == ".txt":
|
| 51 |
+
text = file.read().decode("utf-8")
|
| 52 |
+
pdf = FPDF()
|
| 53 |
+
add_text_as_pdf(pdf, text)
|
| 54 |
+
pdf_output = file_name + ".pdf"
|
| 55 |
+
pdf.output(pdf_output)
|
| 56 |
+
pdfs.append(pdf_output)
|
| 57 |
+
|
| 58 |
+
elif file_extension.lower() == ".docx":
|
| 59 |
+
text = docx_to_text(file)
|
| 60 |
+
pdf = FPDF()
|
| 61 |
+
add_text_as_pdf(pdf, text)
|
| 62 |
+
pdf_output = file_name + ".pdf"
|
| 63 |
+
pdf.output(pdf_output)
|
| 64 |
+
pdfs.append(pdf_output)
|
| 65 |
|
| 66 |
+
elif file_extension.lower() in [".png", ".jpg", ".jpeg"]:
|
| 67 |
+
pdf_output = image_to_pdf(file)
|
| 68 |
+
pdfs.append(pdf_output)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
|
| 70 |
+
# Merge all PDFs into one
|
| 71 |
+
merged_pdf = merge_pdfs(pdfs)
|
| 72 |
+
return merged_pdf
|
| 73 |
|
| 74 |
+
# Hugging Face Gradio Interface
|
| 75 |
+
import gradio as gr
|
| 76 |
|
| 77 |
+
def file_merger(files):
|
| 78 |
+
merged_pdf = process_files(files)
|
| 79 |
+
return merged_pdf
|
| 80 |
|
| 81 |
+
# Define Gradio interface
|
| 82 |
+
iface = gr.Interface(
|
| 83 |
+
fn=file_merger,
|
| 84 |
+
inputs=gr.File(file_count="multiple"),
|
| 85 |
+
outputs="file",
|
| 86 |
+
title="File Merger",
|
| 87 |
+
description="Merge PDF, TXT, DOCX, and Image files into a single PDF."
|
| 88 |
+
)
|
| 89 |
|
| 90 |
+
if __name__ == "__main__":
|
| 91 |
+
iface.launch()
|
|
|
|
|
|
|
|
|
|
|
|