Spaces:
Runtime error
Runtime error
rongo1
commited on
Commit
·
53fe756
1
Parent(s):
9631086
another approach
Browse files- app copy.py +65 -0
- app.py +33 -58
- requirements.txt +4 -4
app copy.py
ADDED
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
# Run the setup script to install unoconv and libreoffice
|
9 |
+
subprocess.run(['bash', 'setup.sh'], check=True)
|
10 |
+
|
11 |
+
# Function to convert PowerPoint to PDF using unoconv
|
12 |
+
def pptx_to_pdf(input_pptx, output_pdf):
|
13 |
+
subprocess.run(['unoconv', '-f', 'pdf', '-o', output_pdf, input_pptx], check=True)
|
14 |
+
|
15 |
+
# Function to convert PDF to PowerPoint
|
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()
|
app.py
CHANGED
@@ -1,65 +1,40 @@
|
|
1 |
-
import os
|
2 |
-
import subprocess
|
3 |
import gradio as gr
|
4 |
from pptx import Presentation
|
5 |
-
from
|
6 |
-
|
7 |
-
|
8 |
-
# Run the setup script to install unoconv and libreoffice
|
9 |
-
subprocess.run(['bash', 'setup.sh'], check=True)
|
10 |
-
|
11 |
-
# Function to convert PowerPoint to PDF using unoconv
|
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 |
-
|
17 |
-
|
18 |
-
|
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 |
-
|
32 |
-
|
33 |
-
left = top = Inches(0)
|
34 |
-
pic = slide.shapes.add_picture(img_path, left, top, width=Inches(10), height=Inches(7.5))
|
35 |
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
|
|
|
|
|
|
|
|
|
|
53 |
)
|
54 |
|
55 |
-
|
56 |
-
|
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()
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
from pptx import Presentation
|
3 |
+
from fpdf import FPDF
|
4 |
+
import tempfile
|
5 |
+
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
+
def pptx_to_pdf(pptx_file):
|
8 |
+
# Create a temporary directory to store intermediate images
|
9 |
+
with tempfile.TemporaryDirectory() as tmpdirname:
|
10 |
+
pdf = FPDF()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
+
# Load the PowerPoint file
|
13 |
+
presentation = Presentation(pptx_file.name)
|
|
|
|
|
14 |
|
15 |
+
for i, slide in enumerate(presentation.slides):
|
16 |
+
# Save each slide as an image
|
17 |
+
img_path = os.path.join(tmpdirname, f"slide_{i}.png")
|
18 |
+
slide.shapes[0].image.save(img_path)
|
19 |
+
|
20 |
+
# Add the image to the PDF
|
21 |
+
pdf.add_page()
|
22 |
+
pdf.image(img_path, 0, 0, 210, 297) # A4 size (210mm x 297mm)
|
23 |
+
|
24 |
+
# Save the PDF to a file
|
25 |
+
pdf_output_path = os.path.join(tmpdirname, "output.pdf")
|
26 |
+
pdf.output(pdf_output_path)
|
27 |
+
|
28 |
+
return pdf_output_path
|
29 |
+
|
30 |
+
# Create a Gradio interface
|
31 |
+
iface = gr.Interface(
|
32 |
+
fn=pptx_to_pdf,
|
33 |
+
inputs=gr.inputs.File(file_types=[".pptx"]),
|
34 |
+
outputs=gr.outputs.File(file_types=[".pdf"]),
|
35 |
+
title="PPTX to PDF Converter",
|
36 |
+
description="Upload a PowerPoint file to convert it to a PDF without watermarks."
|
37 |
)
|
38 |
|
39 |
+
# Launch the app
|
40 |
+
iface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
requirements.txt
CHANGED
@@ -1,4 +1,4 @@
|
|
1 |
-
gradio
|
2 |
-
python-pptx
|
3 |
-
|
4 |
-
|
|
|
1 |
+
gradio==3.1.4
|
2 |
+
python-pptx==0.6.21
|
3 |
+
fpdf==1.7.2
|
4 |
+
Pillow==9.1.1
|