Spaces:
Sleeping
Sleeping
fix bugs
Browse files
app.py
CHANGED
|
@@ -3,7 +3,6 @@ from PIL import Image
|
|
| 3 |
from docx import Document
|
| 4 |
from io import BytesIO
|
| 5 |
import os
|
| 6 |
-
|
| 7 |
def merge_files(file_list, output_filename="merged.pdf"):
|
| 8 |
writer = PdfWriter()
|
| 9 |
|
|
@@ -11,25 +10,31 @@ def merge_files(file_list, output_filename="merged.pdf"):
|
|
| 11 |
ext = file.name.split('.')[-1].lower()
|
| 12 |
|
| 13 |
if ext == 'pdf':
|
| 14 |
-
|
| 15 |
-
|
| 16 |
elif ext in ['jpg', 'jpeg', 'png']:
|
|
|
|
| 17 |
img = Image.open(file)
|
| 18 |
pdf_bytes = BytesIO()
|
| 19 |
img.convert('RGB').save(pdf_bytes, format='PDF')
|
|
|
|
| 20 |
writer.append(pdf_bytes)
|
| 21 |
elif ext == 'docx':
|
|
|
|
| 22 |
doc = Document(file)
|
| 23 |
pdf_bytes = BytesIO()
|
| 24 |
for paragraph in doc.paragraphs:
|
| 25 |
-
pdf_bytes.write(paragraph.text.encode('utf-8'))
|
|
|
|
| 26 |
writer.append(pdf_bytes)
|
| 27 |
elif ext == 'txt':
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
|
|
|
| 32 |
|
|
|
|
| 33 |
with open(output_filename, 'wb') as output_pdf:
|
| 34 |
writer.write(output_pdf)
|
| 35 |
|
|
|
|
| 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 |
|
|
|
|
| 10 |
ext = file.name.split('.')[-1].lower()
|
| 11 |
|
| 12 |
if ext == 'pdf':
|
| 13 |
+
# Read PDF file from memory
|
| 14 |
+
writer.append(file)
|
| 15 |
elif ext in ['jpg', 'jpeg', 'png']:
|
| 16 |
+
# Convert image to PDF
|
| 17 |
img = Image.open(file)
|
| 18 |
pdf_bytes = BytesIO()
|
| 19 |
img.convert('RGB').save(pdf_bytes, format='PDF')
|
| 20 |
+
pdf_bytes.seek(0)
|
| 21 |
writer.append(pdf_bytes)
|
| 22 |
elif ext == 'docx':
|
| 23 |
+
# Convert DOCX to PDF-like content
|
| 24 |
doc = Document(file)
|
| 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
|
| 32 |
+
pdf_bytes = BytesIO()
|
| 33 |
+
pdf_bytes.write(file.read())
|
| 34 |
+
pdf_bytes.seek(0)
|
| 35 |
+
writer.append(pdf_bytes)
|
| 36 |
|
| 37 |
+
# Save the final PDF
|
| 38 |
with open(output_filename, 'wb') as output_pdf:
|
| 39 |
writer.write(output_pdf)
|
| 40 |
|