RanaZaeem12 commited on
Commit
82c991c
Β·
verified Β·
1 Parent(s): 6392c48

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +148 -142
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(file_obj, output_format):
12
- name = file_obj.name
13
- ext = name.split(".")[-1].lower()
14
- file_bytes = file_obj.read()
15
-
16
- def to_pdf():
17
- buf = BytesIO()
18
- if ext in ["png", "jpg", "jpeg"]:
19
- img = Image.open(BytesIO(file_bytes)).convert("RGB")
20
- img.save(buf, format="PDF")
21
- elif ext == "docx":
22
- doc = Document(BytesIO(file_bytes))
23
- c = canvas.Canvas(buf)
24
- for i, para in enumerate(doc.paragraphs):
25
- c.drawString(10, 800 - 15 * i, para.text)
26
- c.save()
27
- elif ext == "pptx":
28
- ppt = Presentation(BytesIO(file_bytes))
29
- c = canvas.Canvas(buf)
30
- y = 800
31
- for slide in ppt.slides:
32
- for shape in slide.shapes:
33
- if hasattr(shape, "text"):
34
- c.drawString(10, y, shape.text)
35
- y -= 15
36
- c.save()
37
- elif ext == "xlsx":
38
- df = pd.read_excel(BytesIO(file_bytes))
39
- c = canvas.Canvas(buf)
40
- for i, row in df.iterrows():
41
- c.drawString(10, 800 - i * 15, str(row.to_list()))
42
- c.save()
43
- elif ext == "pdf":
44
- return file_bytes, "converted.pdf"
45
- return buf.getvalue(), "converted.pdf"
46
-
47
- def to_word():
48
- doc = Document()
49
- if ext in ["png", "jpg", "jpeg"]:
50
- image = Image.open(BytesIO(file_bytes))
51
- image.save("temp_img.jpg")
52
- doc.add_picture("temp_img.jpg")
53
- elif ext == "pdf":
54
- pdf = fitz.open(stream=file_bytes, filetype="pdf")
55
- for page in pdf:
56
- doc.add_paragraph(page.get_text())
57
- elif ext == "pptx":
58
- ppt = Presentation(BytesIO(file_bytes))
59
- for slide in ppt.slides:
60
- for shape in slide.shapes:
61
- if hasattr(shape, "text"):
62
- doc.add_paragraph(shape.text)
63
- elif ext == "xlsx":
64
- df = pd.read_excel(BytesIO(file_bytes))
65
- for _, row in df.iterrows():
66
- doc.add_paragraph(" | ".join(map(str, row)))
67
- elif ext == "docx":
68
- return file_bytes, name
69
- buf = BytesIO()
70
- doc.save(buf)
71
- return buf.getvalue(), "converted.docx"
72
-
73
- def to_excel():
74
- if ext == "xlsx":
75
- return file_bytes, name
76
- df = pd.DataFrame()
77
- if ext in ["png", "jpg", "jpeg"]:
78
- df = pd.DataFrame([["Image file", name]])
79
- elif ext == "pdf":
80
- pdf = fitz.open(stream=file_bytes, filetype="pdf")
81
- df = pd.DataFrame([page.get_text() for page in pdf], columns=["Text"])
82
- elif ext == "docx":
83
- doc = Document(BytesIO(file_bytes))
84
- df = pd.DataFrame([[para.text] for para in doc.paragraphs], columns=["Text"])
85
- elif ext == "pptx":
86
- ppt = Presentation(BytesIO(file_bytes))
87
- texts = []
88
- for slide in ppt.slides:
89
- text = "\n".join([shape.text for shape in slide.shapes if hasattr(shape, "text")])
90
- texts.append([text])
91
- df = pd.DataFrame(texts, columns=["Slide Text"])
92
- buf = BytesIO()
93
- df.to_excel(buf, index=False)
94
- return buf.getvalue(), "converted.xlsx"
95
-
96
- def to_ppt():
97
- prs = Presentation()
98
- slide_layout = prs.slide_layouts[1]
99
- if ext == "pdf":
100
- pdf = fitz.open(stream=file_bytes, filetype="pdf")
101
- for page in pdf:
102
- slide = prs.slides.add_slide(slide_layout)
103
- slide.shapes.title.text = "Page"
104
- slide.placeholders[1].text = page.get_text()
105
- elif ext == "docx":
106
- doc = Document(BytesIO(file_bytes))
107
- for para in doc.paragraphs:
108
- slide = prs.slides.add_slide(slide_layout)
109
- slide.shapes.title.text = "Paragraph"
110
- slide.placeholders[1].text = para.text
111
- elif ext == "xlsx":
112
- df = pd.read_excel(BytesIO(file_bytes))
113
- for i, row in df.iterrows():
 
 
 
 
 
 
 
114
  slide = prs.slides.add_slide(slide_layout)
115
- slide.shapes.title.text = f"Row {i}"
116
- slide.placeholders[1].text = " | ".join(map(str, row))
117
- elif ext in ["png", "jpg", "jpeg"]:
118
- slide = prs.slides.add_slide(slide_layout)
119
- slide.shapes.title.text = "Image"
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
- images[0].save(buf, format="PNG")
134
- return buf.getvalue(), "converted.png"
135
- return None, None
136
-
137
- converters = {
138
- "PDF": to_pdf,
139
- "Word": to_word,
140
- "Excel": to_excel,
141
- "PowerPoint": to_ppt,
142
- "HD Image": to_image
143
- }
144
-
145
- try:
146
- data, filename = converters[output_format]()
147
- return filename, data
148
- except Exception as e:
149
- return "Error.txt", str(e).encode()
 
 
 
 
 
 
 
 
 
 
 
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 File",
167
  file_types=[".png", ".jpg", ".jpeg", ".pdf", ".docx", ".pptx", ".xlsx"],
168
- file_count="single"
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
- output_file = gr.File(label="πŸ“₯ Download Converted File")
179
 
180
- convert_btn.click(fn=convert_file, inputs=[file_input, output_format], outputs=output_file)
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/)")