Ze101 commited on
Commit
f2af1db
·
verified ·
1 Parent(s): 8291f0f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -14
app.py CHANGED
@@ -4,7 +4,8 @@ from PyPDF2 import PdfMerger
4
  from docx import Document
5
  from PIL import Image
6
  import gradio as gr
7
- import comtypes.client
 
8
 
9
  # Function to add a single text input as a new page in an FPDF object
10
  def add_text_as_page(pdf, text):
@@ -30,6 +31,10 @@ def merge_pdfs(pdfs):
30
  merger.close()
31
  return output_path
32
 
 
 
 
 
33
  # Main function to process uploaded files and manual text inputs
34
  def process_files_and_text(files, manual_texts):
35
  pdfs = []
@@ -48,18 +53,8 @@ def process_files_and_text(files, manual_texts):
48
  add_text_as_page(pdf, text)
49
 
50
  elif file_extension.lower() == ".docx":
51
- wdFormatPDF = 17
52
-
53
- in_file = file.name
54
- out_file = file_name + ".pdf"
55
-
56
- word = comtypes.client.CreateObject('Word.Application')
57
- doc = word.Documents.Open(in_file)
58
- doc.SaveAs(out_file, FileFormat=wdFormatPDF)
59
- doc.Close()
60
- word.Quit()
61
- pdfs.append(out_file)
62
-
63
  elif file_extension.lower() in [".png", ".jpg", ".jpeg"]:
64
  pdf_output = image_to_pdf(file.name)
65
  pdfs.append(pdf_output)
@@ -114,4 +109,4 @@ with gr.Blocks() as iface:
114
  # Merge files and text
115
  merge_button.click(file_merger, inputs=[file_input, manual_texts_state], outputs=output)
116
 
117
- iface.launch()
 
4
  from docx import Document
5
  from PIL import Image
6
  import gradio as gr
7
+ import aspose.words as aw
8
+
9
 
10
  # Function to add a single text input as a new page in an FPDF object
11
  def add_text_as_page(pdf, text):
 
31
  merger.close()
32
  return output_path
33
 
34
+ def docx_to_pdf(docx_file, pdf_file):
35
+ doc = aw.Document(docx_file)
36
+ doc.save(pdf_file)
37
+
38
  # Main function to process uploaded files and manual text inputs
39
  def process_files_and_text(files, manual_texts):
40
  pdfs = []
 
53
  add_text_as_page(pdf, text)
54
 
55
  elif file_extension.lower() == ".docx":
56
+ docx_to_pdf(file.name, file_name + ".pdf")
57
+ pdfs.append(file_name + ".pdf")
 
 
 
 
 
 
 
 
 
 
58
  elif file_extension.lower() in [".png", ".jpg", ".jpeg"]:
59
  pdf_output = image_to_pdf(file.name)
60
  pdfs.append(pdf_output)
 
109
  # Merge files and text
110
  merge_button.click(file_merger, inputs=[file_input, manual_texts_state], outputs=output)
111
 
112
+ iface.launch()