Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -7,146 +7,153 @@ from reportlab.pdfgen import canvas
|
|
7 |
from docx import Document
|
8 |
from pptx import Presentation
|
9 |
import fitz # PyMuPDF
|
|
|
10 |
|
11 |
-
def convert_file(
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
c
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
c
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
texts
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
114 |
slide = prs.slides.add_slide(slide_layout)
|
115 |
-
slide.shapes.title.text =
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
img_path = f"img_{name}"
|
121 |
-
Image.open(BytesIO(file_bytes)).save(img_path)
|
122 |
-
slide.shapes.add_picture(img_path, 100, 100, width=400, height=300)
|
123 |
-
buf = BytesIO()
|
124 |
-
prs.save(buf)
|
125 |
-
return buf.getvalue(), "converted.pptx"
|
126 |
-
|
127 |
-
def to_image():
|
128 |
-
if ext in ["png", "jpg", "jpeg"]:
|
129 |
-
return file_bytes, "converted.png"
|
130 |
-
elif ext == "pdf":
|
131 |
-
images = convert_from_bytes(file_bytes)
|
132 |
buf = BytesIO()
|
133 |
-
|
134 |
-
return buf.getvalue(), "converted.
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
150 |
|
151 |
with gr.Blocks(title="Universal File Converter", theme=gr.themes.Soft()) as demo:
|
152 |
gr.Markdown(
|
@@ -155,7 +162,6 @@ with gr.Blocks(title="Universal File Converter", theme=gr.themes.Soft()) as demo
|
|
155 |
Convert your **images**, **documents**, and **presentations** to π PDF, π Word, π Excel, π½οΈ PowerPoint, or πΌοΈ HD Image!
|
156 |
|
157 |
> Supported formats: `.jpg`, `.png`, `.pdf`, `.docx`, `.pptx`, `.xlsx`
|
158 |
-
|
159 |
---
|
160 |
"""
|
161 |
)
|
@@ -163,9 +169,9 @@ with gr.Blocks(title="Universal File Converter", theme=gr.themes.Soft()) as demo
|
|
163 |
with gr.Row():
|
164 |
with gr.Column(scale=1):
|
165 |
file_input = gr.File(
|
166 |
-
label="π€ Upload
|
167 |
file_types=[".png", ".jpg", ".jpeg", ".pdf", ".docx", ".pptx", ".xlsx"],
|
168 |
-
file_count="
|
169 |
)
|
170 |
output_format = gr.Radio(
|
171 |
["PDF", "Word", "Excel", "PowerPoint", "HD Image"],
|
@@ -175,9 +181,9 @@ with gr.Blocks(title="Universal File Converter", theme=gr.themes.Soft()) as demo
|
|
175 |
convert_btn = gr.Button("π Convert Now")
|
176 |
|
177 |
with gr.Column(scale=1):
|
178 |
-
|
179 |
|
180 |
-
convert_btn.click(fn=convert_file, inputs=[file_input, output_format], outputs=
|
181 |
|
182 |
gr.Markdown("---")
|
183 |
gr.Markdown("Made with β€οΈ using [Gradio](https://gradio.app/)")
|
|
|
7 |
from docx import Document
|
8 |
from pptx import Presentation
|
9 |
import fitz # PyMuPDF
|
10 |
+
import os
|
11 |
|
12 |
+
def convert_file(files, output_format):
|
13 |
+
results = []
|
14 |
+
for file_obj in files:
|
15 |
+
name = file_obj.name
|
16 |
+
ext = name.split(".")[-1].lower()
|
17 |
+
file_bytes = file_obj.read()
|
18 |
+
|
19 |
+
def to_pdf():
|
20 |
+
buf = BytesIO()
|
21 |
+
if ext in ["png", "jpg", "jpeg"]:
|
22 |
+
img = Image.open(BytesIO(file_bytes)).convert("RGB")
|
23 |
+
img.save(buf, format="PDF")
|
24 |
+
elif ext == "docx":
|
25 |
+
doc = Document(BytesIO(file_bytes))
|
26 |
+
c = canvas.Canvas(buf)
|
27 |
+
for i, para in enumerate(doc.paragraphs):
|
28 |
+
c.drawString(10, 800 - 15 * i, para.text)
|
29 |
+
c.save()
|
30 |
+
elif ext == "pptx":
|
31 |
+
ppt = Presentation(BytesIO(file_bytes))
|
32 |
+
c = canvas.Canvas(buf)
|
33 |
+
y = 800
|
34 |
+
for slide in ppt.slides:
|
35 |
+
for shape in slide.shapes:
|
36 |
+
if hasattr(shape, "text"):
|
37 |
+
c.drawString(10, y, shape.text)
|
38 |
+
y -= 15
|
39 |
+
c.save()
|
40 |
+
elif ext == "xlsx":
|
41 |
+
df = pd.read_excel(BytesIO(file_bytes))
|
42 |
+
c = canvas.Canvas(buf)
|
43 |
+
for i, row in df.iterrows():
|
44 |
+
c.drawString(10, 800 - i * 15, str(row.to_list()))
|
45 |
+
c.save()
|
46 |
+
elif ext == "pdf":
|
47 |
+
return file_bytes, "converted.pdf"
|
48 |
+
return buf.getvalue(), "converted.pdf"
|
49 |
+
|
50 |
+
def to_word():
|
51 |
+
doc = Document()
|
52 |
+
if ext in ["png", "jpg", "jpeg"]:
|
53 |
+
image = Image.open(BytesIO(file_bytes))
|
54 |
+
image.save("temp_img.jpg")
|
55 |
+
doc.add_picture("temp_img.jpg")
|
56 |
+
os.remove("temp_img.jpg")
|
57 |
+
elif ext == "pdf":
|
58 |
+
pdf = fitz.open(stream=file_bytes, filetype="pdf")
|
59 |
+
for page in pdf:
|
60 |
+
doc.add_paragraph(page.get_text())
|
61 |
+
elif ext == "pptx":
|
62 |
+
ppt = Presentation(BytesIO(file_bytes))
|
63 |
+
for slide in ppt.slides:
|
64 |
+
for shape in slide.shapes:
|
65 |
+
if hasattr(shape, "text"):
|
66 |
+
doc.add_paragraph(shape.text)
|
67 |
+
elif ext == "xlsx":
|
68 |
+
df = pd.read_excel(BytesIO(file_bytes))
|
69 |
+
for _, row in df.iterrows():
|
70 |
+
doc.add_paragraph(" | ".join(map(str, row)))
|
71 |
+
elif ext == "docx":
|
72 |
+
return file_bytes, name
|
73 |
+
buf = BytesIO()
|
74 |
+
doc.save(buf)
|
75 |
+
return buf.getvalue(), "converted.docx"
|
76 |
+
|
77 |
+
def to_excel():
|
78 |
+
if ext == "xlsx":
|
79 |
+
return file_bytes, name
|
80 |
+
df = pd.DataFrame()
|
81 |
+
if ext in ["png", "jpg", "jpeg"]:
|
82 |
+
df = pd.DataFrame([["Image file", name]])
|
83 |
+
elif ext == "pdf":
|
84 |
+
pdf = fitz.open(stream=file_bytes, filetype="pdf")
|
85 |
+
df = pd.DataFrame([page.get_text() for page in pdf], columns=["Text"])
|
86 |
+
elif ext == "docx":
|
87 |
+
doc = Document(BytesIO(file_bytes))
|
88 |
+
df = pd.DataFrame([[para.text] for para in doc.paragraphs], columns=["Text"])
|
89 |
+
elif ext == "pptx":
|
90 |
+
ppt = Presentation(BytesIO(file_bytes))
|
91 |
+
texts = []
|
92 |
+
for slide in ppt.slides:
|
93 |
+
text = "\n".join([shape.text for shape in slide.shapes if hasattr(shape, "text")])
|
94 |
+
texts.append([text])
|
95 |
+
df = pd.DataFrame(texts, columns=["Slide Text"])
|
96 |
+
buf = BytesIO()
|
97 |
+
df.to_excel(buf, index=False)
|
98 |
+
return buf.getvalue(), "converted.xlsx"
|
99 |
+
|
100 |
+
def to_ppt():
|
101 |
+
prs = Presentation()
|
102 |
+
slide_layout = prs.slide_layouts[1]
|
103 |
+
if ext == "pdf":
|
104 |
+
pdf = fitz.open(stream=file_bytes, filetype="pdf")
|
105 |
+
for page in pdf:
|
106 |
+
slide = prs.slides.add_slide(slide_layout)
|
107 |
+
slide.shapes.title.text = "Page"
|
108 |
+
slide.placeholders[1].text = page.get_text()
|
109 |
+
elif ext == "docx":
|
110 |
+
doc = Document(BytesIO(file_bytes))
|
111 |
+
for para in doc.paragraphs:
|
112 |
+
slide = prs.slides.add_slide(slide_layout)
|
113 |
+
slide.shapes.title.text = "Paragraph"
|
114 |
+
slide.placeholders[1].text = para.text
|
115 |
+
elif ext == "xlsx":
|
116 |
+
df = pd.read_excel(BytesIO(file_bytes))
|
117 |
+
for i, row in df.iterrows():
|
118 |
+
slide = prs.slides.add_slide(slide_layout)
|
119 |
+
slide.shapes.title.text = f"Row {i}"
|
120 |
+
slide.placeholders[1].text = " | ".join(map(str, row))
|
121 |
+
elif ext in ["png", "jpg", "jpeg"]:
|
122 |
slide = prs.slides.add_slide(slide_layout)
|
123 |
+
slide.shapes.title.text = "Image"
|
124 |
+
img_path = f"img_{name}"
|
125 |
+
Image.open(BytesIO(file_bytes)).save(img_path)
|
126 |
+
slide.shapes.add_picture(img_path, 100, 100, width=400, height=300)
|
127 |
+
os.remove(img_path)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
128 |
buf = BytesIO()
|
129 |
+
prs.save(buf)
|
130 |
+
return buf.getvalue(), "converted.pptx"
|
131 |
+
|
132 |
+
def to_image():
|
133 |
+
if ext in ["png", "jpg", "jpeg"]:
|
134 |
+
return file_bytes, "converted.png"
|
135 |
+
elif ext == "pdf":
|
136 |
+
images = convert_from_bytes(file_bytes)
|
137 |
+
buf = BytesIO()
|
138 |
+
images[0].save(buf, format="PNG")
|
139 |
+
return buf.getvalue(), "converted.png"
|
140 |
+
return None, None
|
141 |
+
|
142 |
+
converters = {
|
143 |
+
"PDF": to_pdf,
|
144 |
+
"Word": to_word,
|
145 |
+
"Excel": to_excel,
|
146 |
+
"PowerPoint": to_ppt,
|
147 |
+
"HD Image": to_image
|
148 |
+
}
|
149 |
+
|
150 |
+
try:
|
151 |
+
data, filename = converters[output_format]()
|
152 |
+
results.append((filename, data))
|
153 |
+
except Exception as e:
|
154 |
+
results.append(("Error.txt", str(e).encode()))
|
155 |
+
|
156 |
+
return [(fname, BytesIO(content)) for fname, content in results]
|
157 |
|
158 |
with gr.Blocks(title="Universal File Converter", theme=gr.themes.Soft()) as demo:
|
159 |
gr.Markdown(
|
|
|
162 |
Convert your **images**, **documents**, and **presentations** to π PDF, π Word, π Excel, π½οΈ PowerPoint, or πΌοΈ HD Image!
|
163 |
|
164 |
> Supported formats: `.jpg`, `.png`, `.pdf`, `.docx`, `.pptx`, `.xlsx`
|
|
|
165 |
---
|
166 |
"""
|
167 |
)
|
|
|
169 |
with gr.Row():
|
170 |
with gr.Column(scale=1):
|
171 |
file_input = gr.File(
|
172 |
+
label="π€ Upload Files",
|
173 |
file_types=[".png", ".jpg", ".jpeg", ".pdf", ".docx", ".pptx", ".xlsx"],
|
174 |
+
file_count="multiple"
|
175 |
)
|
176 |
output_format = gr.Radio(
|
177 |
["PDF", "Word", "Excel", "PowerPoint", "HD Image"],
|
|
|
181 |
convert_btn = gr.Button("π Convert Now")
|
182 |
|
183 |
with gr.Column(scale=1):
|
184 |
+
output_files = gr.Files(label="π₯ Download Converted Files")
|
185 |
|
186 |
+
convert_btn.click(fn=convert_file, inputs=[file_input, output_format], outputs=output_files)
|
187 |
|
188 |
gr.Markdown("---")
|
189 |
gr.Markdown("Made with β€οΈ using [Gradio](https://gradio.app/)")
|