rongo1 commited on
Commit
22543a3
·
1 Parent(s): 79dcd5a

back to old

Browse files
Files changed (2) hide show
  1. app.py +58 -34
  2. output.pdf +0 -0
app.py CHANGED
@@ -1,41 +1,65 @@
 
 
1
  import gradio as gr
2
  from pptx import Presentation
3
- from fpdf import FPDF
4
- import tempfile
5
- import os
6
- from PIL import Image
 
 
 
 
 
 
7
 
8
- def pptx_to_pdf(pptx_file):
9
- # Create a temporary directory to store intermediate images
10
- with tempfile.TemporaryDirectory() as tmpdirname:
11
- pdf = FPDF()
 
 
 
 
 
 
 
 
 
 
12
 
13
- # Load the PowerPoint file
14
- presentation = Presentation(pptx_file.name)
 
 
15
 
16
- for i, slide in enumerate(presentation.slides):
17
- # Save each slide as an image
18
- img_path = os.path.join(tmpdirname, f"slide_{i}.png")
19
- slide.shapes[0].image.save(img_path)
20
-
21
- # Add the image to the PDF
22
- pdf.add_page()
23
- pdf.image(img_path, 0, 0, 210, 297) # A4 size (210mm x 297mm)
24
-
25
- # Save the PDF to a file
26
- pdf_output_path = os.path.join(tmpdirname, "output.pdf")
27
- pdf.output(pdf_output_path)
28
-
29
- return pdf_output_path
30
-
31
- # Create a Gradio interface
32
- iface = gr.Interface(
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
- # Launch the app
41
- iface.launch()
 
 
 
 
 
 
 
 
 
 
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