Spaces:
Runtime error
Runtime error
rongo1
commited on
Commit
·
22543a3
1
Parent(s):
79dcd5a
back to old
Browse files- app.py +58 -34
- output.pdf +0 -0
app.py
CHANGED
@@ -1,41 +1,65 @@
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
from pptx import Presentation
|
3 |
-
from
|
4 |
-
import
|
5 |
-
|
6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
-
def
|
9 |
-
#
|
10 |
-
|
11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
-
#
|
14 |
-
|
|
|
|
|
15 |
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
fn=pptx_to_pdf,
|
34 |
-
inputs=gr.File(file_types=[".pptx"]),
|
35 |
-
outputs=gr.File(file_types=[".pdf"]),
|
36 |
-
title="PPTX to PDF Converter",
|
37 |
-
description="Upload a PowerPoint file to convert it to a PDF without watermarks."
|
38 |
)
|
39 |
|
40 |
-
|
41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import subprocess
|
3 |
import gradio as gr
|
4 |
from pptx import Presentation
|
5 |
+
from pptx.util import Inches
|
6 |
+
from pdf2image import convert_from_path
|
7 |
+
|
8 |
+
|
9 |
+
subprocess.run(['bash', 'setup.sh'], check=True)
|
10 |
+
|
11 |
+
|
12 |
+
def pptx_to_pdf(input_pptx, output_pdf):
|
13 |
+
subprocess.run(['unoconv', '-f', 'pdf', '-o', output_pdf, input_pptx], check=True)
|
14 |
+
|
15 |
|
16 |
+
def pdf_to_pptx(input_pdf, output_pptx):
|
17 |
+
# Specify a directory to save images
|
18 |
+
output_dir = "pdf_images"
|
19 |
+
os.makedirs(output_dir, exist_ok=True)
|
20 |
+
|
21 |
+
# Convert PDF to images and save in the specified directory
|
22 |
+
images = convert_from_path(input_pdf, output_folder=output_dir)
|
23 |
+
|
24 |
+
# Create a new PowerPoint presentation
|
25 |
+
presentation = Presentation()
|
26 |
+
|
27 |
+
for i, img in enumerate(images):
|
28 |
+
img_path = os.path.join(output_dir, f"page_{i+1}.png")
|
29 |
+
img.save(img_path, 'PNG')
|
30 |
|
31 |
+
slide_layout = presentation.slide_layouts[5] # Use a blank layout
|
32 |
+
slide = presentation.slides.add_slide(slide_layout)
|
33 |
+
left = top = Inches(0)
|
34 |
+
pic = slide.shapes.add_picture(img_path, left, top, width=Inches(10), height=Inches(7.5))
|
35 |
|
36 |
+
presentation.save(output_pptx)
|
37 |
+
|
38 |
+
def convert_pptx_to_pdf(pptx_file):
|
39 |
+
output_pdf = "output.pdf"
|
40 |
+
pptx_to_pdf(pptx_file.name, output_pdf)
|
41 |
+
return output_pdf
|
42 |
+
|
43 |
+
def convert_pdf_to_pptx(pdf_file):
|
44 |
+
output_pptx = "output_converted.pptx"
|
45 |
+
pdf_to_pptx(pdf_file.name, output_pptx)
|
46 |
+
return output_pptx
|
47 |
+
|
48 |
+
pptx_to_pdf_interface = gr.Interface(
|
49 |
+
fn=convert_pptx_to_pdf,
|
50 |
+
inputs=gr.File(label="Upload PPTX File", file_count="single"),
|
51 |
+
outputs=gr.File(label="Download PDF File"),
|
52 |
+
title="Convert PPTX to PDF"
|
|
|
|
|
|
|
|
|
|
|
53 |
)
|
54 |
|
55 |
+
pdf_to_pptx_interface = gr.Interface(
|
56 |
+
fn=convert_pdf_to_pptx,
|
57 |
+
inputs=gr.File(label="Upload PDF File", file_count="single"),
|
58 |
+
outputs=gr.File(label="Download PPTX File"),
|
59 |
+
title="Convert PDF to PPTX"
|
60 |
+
)
|
61 |
+
|
62 |
+
app = gr.TabbedInterface([pptx_to_pdf_interface, pdf_to_pptx_interface], ["PPTX to PDF", "PDF to PPTX"])
|
63 |
+
|
64 |
+
if __name__ == "__main__":
|
65 |
+
app.launch()
|
output.pdf
ADDED
Binary file (169 kB). View file
|
|