Ze101 commited on
Commit
9d868df
·
verified ·
1 Parent(s): 104fef3

fix docx error

Browse files
Files changed (1) hide show
  1. app.py +77 -66
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
- class PDF(FPDF):
9
- def __init__(self):
10
- super().__init__()
11
- self.add_page()
12
- self.set_auto_page_break(auto=True, margin=15)
13
- # Add a Unicode-compatible font
14
- self.add_font("DejaVu", "", "DejaVuSans.ttf", uni=True)
15
- self.set_font("DejaVu", size=12)
16
 
17
- def add_unicode_text(self, text):
18
- # Handle multi-cell text with special characters
19
- self.multi_cell(0, 10, text)
 
 
 
20
 
21
- def docx_to_pdf(docx_file):
22
- """
23
- Convert a DOCX file into a simple PDF format, with Unicode support.
24
- """
25
- document = Document(docx_file)
26
- pdf = PDF()
27
 
28
- for paragraph in document.paragraphs:
29
- text = paragraph.text.strip()
30
- if text:
31
- pdf.add_unicode_text(text)
 
 
 
 
 
32
 
33
- # Save to BytesIO
34
- pdf_bytes = BytesIO()
35
- pdf.output(pdf_bytes)
36
- pdf_bytes.seek(0)
37
- return pdf_bytes
38
 
39
- def merge_files(file_list, output_filename="merged.pdf"):
40
- writer = PdfWriter()
41
-
42
- for file in file_list:
43
- ext = file.name.split('.')[-1].lower()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44
 
45
- if ext == 'pdf':
46
- # Read PDF file from memory
47
- writer.append(file)
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
- # Save the final PDF
67
- with open(output_filename, 'wb') as output_pdf:
68
- writer.write(output_pdf)
69
 
70
- return output_filename
 
71
 
 
 
 
72
 
73
- import streamlit as st
 
 
 
 
 
 
 
74
 
75
- uploaded_files = st.file_uploader("Upload files to merge", type=["pdf", "docx", "txt", "jpg", "png"], accept_multiple_files=True)
76
- if st.button("Merge"):
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()