innoai commited on
Commit
cf01dad
·
verified ·
1 Parent(s): 0e9357d

Upload pptx2pdf_web_en.py

Browse files
Files changed (1) hide show
  1. pptx2pdf_web_en.py +25 -3
pptx2pdf_web_en.py CHANGED
@@ -44,7 +44,10 @@ def make_zip_file(files, zip_path):
44
  return zip_path
45
 
46
  def pptx2pdf_web(pptx_file): # pptx_file is the Gradio file object
47
- with tempfile.TemporaryDirectory() as tmpdir:
 
 
 
48
  gradio_temp_file_path = pptx_file.name # Path to the file uploaded by Gradio
49
 
50
  # Create a path for our copy of the PPTX inside our own tmpdir
@@ -59,10 +62,29 @@ def pptx2pdf_web(pptx_file): # pptx_file is the Gradio file object
59
  # convert_pptx_to_pdf will create the PDF also within tmpdir.
60
  pdf_path = convert_pptx_to_pdf(our_copy_of_pptx_path, tmpdir)
61
 
62
- # Package
 
 
 
 
 
 
 
 
 
 
63
  zip_path = os.path.join(tmpdir, 'converted.zip')
64
  make_zip_file([pdf_path], zip_path)
65
- return pdf_path, zip_path
 
 
 
 
 
 
 
 
 
66
 
67
  with gr.Blocks(title="PPTX to PDF Converter - Easily Convert PowerPoint to PDF") as demo:
68
  gr.Markdown("# PPTX to PDF Converter") # Added main title here
 
44
  return zip_path
45
 
46
  def pptx2pdf_web(pptx_file): # pptx_file is the Gradio file object
47
+ # Create a temporary directory that won't be auto-deleted
48
+ tmpdir = tempfile.mkdtemp()
49
+
50
+ try:
51
  gradio_temp_file_path = pptx_file.name # Path to the file uploaded by Gradio
52
 
53
  # Create a path for our copy of the PPTX inside our own tmpdir
 
62
  # convert_pptx_to_pdf will create the PDF also within tmpdir.
63
  pdf_path = convert_pptx_to_pdf(our_copy_of_pptx_path, tmpdir)
64
 
65
+ # Create persistent copies of the output files for Gradio
66
+ import tempfile as tf
67
+
68
+ # Create persistent PDF file
69
+ pdf_fd, persistent_pdf_path = tf.mkstemp(suffix='.pdf', prefix='converted_')
70
+ os.close(pdf_fd)
71
+ shutil.copy2(pdf_path, persistent_pdf_path)
72
+
73
+ # Create persistent ZIP file
74
+ zip_fd, persistent_zip_path = tf.mkstemp(suffix='.zip', prefix='converted_')
75
+ os.close(zip_fd)
76
  zip_path = os.path.join(tmpdir, 'converted.zip')
77
  make_zip_file([pdf_path], zip_path)
78
+ shutil.copy2(zip_path, persistent_zip_path)
79
+
80
+ return persistent_pdf_path, persistent_zip_path
81
+
82
+ finally:
83
+ # Clean up our temporary directory
84
+ try:
85
+ shutil.rmtree(tmpdir)
86
+ except:
87
+ pass # Ignore cleanup errors
88
 
89
  with gr.Blocks(title="PPTX to PDF Converter - Easily Convert PowerPoint to PDF") as demo:
90
  gr.Markdown("# PPTX to PDF Converter") # Added main title here