Spaces:
Sleeping
Sleeping
fix docx bug
Browse files
app.py
CHANGED
|
@@ -3,6 +3,29 @@ from PIL import Image
|
|
| 3 |
from docx import Document
|
| 4 |
from io import BytesIO
|
| 5 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
def merge_files(file_list, output_filename="merged.pdf"):
|
| 7 |
writer = PdfWriter()
|
| 8 |
|
|
@@ -21,11 +44,7 @@ def merge_files(file_list, output_filename="merged.pdf"):
|
|
| 21 |
writer.append(pdf_bytes)
|
| 22 |
elif ext == 'docx':
|
| 23 |
# Convert DOCX to PDF-like content
|
| 24 |
-
|
| 25 |
-
pdf_bytes = BytesIO()
|
| 26 |
-
for paragraph in doc.paragraphs:
|
| 27 |
-
pdf_bytes.write((paragraph.text + '\n').encode('utf-8'))
|
| 28 |
-
pdf_bytes.seek(0)
|
| 29 |
writer.append(pdf_bytes)
|
| 30 |
elif ext == 'txt':
|
| 31 |
# Convert TXT file to PDF-like content
|
|
|
|
| 3 |
from docx import Document
|
| 4 |
from io import BytesIO
|
| 5 |
import os
|
| 6 |
+
from fpdf import FPDF
|
| 7 |
+
|
| 8 |
+
def docx_to_pdf(docx_file):
|
| 9 |
+
"""
|
| 10 |
+
Convert a DOCX file into a simple PDF format.
|
| 11 |
+
"""
|
| 12 |
+
document = Document(docx_file)
|
| 13 |
+
pdf = FPDF()
|
| 14 |
+
pdf.set_auto_page_break(auto=True, margin=15)
|
| 15 |
+
pdf.add_page()
|
| 16 |
+
pdf.set_font("Arial", size=12)
|
| 17 |
+
|
| 18 |
+
# Extract and add text from DOCX to the PDF
|
| 19 |
+
for paragraph in document.paragraphs:
|
| 20 |
+
text = paragraph.text.strip()
|
| 21 |
+
if text:
|
| 22 |
+
pdf.multi_cell(0, 10, text)
|
| 23 |
+
|
| 24 |
+
pdf_bytes = BytesIO()
|
| 25 |
+
pdf.output(pdf_bytes)
|
| 26 |
+
pdf_bytes.seek(0) # Reset pointer
|
| 27 |
+
return pdf_bytes
|
| 28 |
+
|
| 29 |
def merge_files(file_list, output_filename="merged.pdf"):
|
| 30 |
writer = PdfWriter()
|
| 31 |
|
|
|
|
| 44 |
writer.append(pdf_bytes)
|
| 45 |
elif ext == 'docx':
|
| 46 |
# Convert DOCX to PDF-like content
|
| 47 |
+
pdf_bytes = docx_to_pdf(file)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
writer.append(pdf_bytes)
|
| 49 |
elif ext == 'txt':
|
| 50 |
# Convert TXT file to PDF-like content
|