prithivMLmods commited on
Commit
a9bbdc9
·
verified ·
1 Parent(s): b7e2217

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -37
app.py CHANGED
@@ -3,48 +3,72 @@ from pdf2docx import Converter
3
  from docx import Document
4
  from fpdf import FPDF
5
  import os
 
6
 
7
  title_and_description = """
8
- # PDF to Word and Word to PDF
 
 
9
  """
10
 
11
  def pdf_to_word(pdf_file):
12
- docx_filename = pdf_file.name.replace('.pdf', '.docx')
13
-
14
- cv = Converter(pdf_file.name)
15
- cv.convert(docx_filename, multi_processing=True, start=0, end=None)
16
- cv.close()
17
-
18
- return docx_filename
 
 
 
 
 
 
 
 
 
 
19
 
20
  def word_to_pdf(docx_file):
21
- pdf_filename = "output.pdf"
22
-
23
- doc = Document(docx_file.name)
24
- pdf = FPDF(format='A4')
25
- pdf.set_auto_page_break(auto=True, margin=15)
26
- pdf.add_page()
27
- pdf.add_font('Arial', '', 'Arial.ttf', uni=True)
28
- pdf.set_font('Arial', size=12)
 
 
 
 
 
 
 
29
 
30
- for para in doc.paragraphs:
31
- text = para.text.strip()
32
- if not text:
33
- continue
34
 
35
- words = text.split()
36
- line = ''
37
- for word in words:
38
- if pdf.get_string_width(line + word) < (pdf.w - 2 * pdf.l_margin):
39
- line += word + ' '
40
- else:
41
- pdf.cell(0, 10, line, ln=True)
42
- line = word + ' '
43
- if line:
44
- pdf.cell(0, 10, line, ln=True)
45
 
46
- pdf.output(pdf_filename)
47
- return pdf_filename
 
 
 
 
48
 
49
  with gr.Blocks() as app:
50
  gr.Markdown(title_and_description)
@@ -52,18 +76,18 @@ with gr.Blocks() as app:
52
  with gr.Row():
53
  with gr.Column():
54
  with gr.Accordion("PDF to Word"):
55
- pdf_input = gr.File(label="Upload PDF")
56
  convert_pdf_to_word = gr.Button("Convert to Word")
57
- word_output = gr.File(label="Download Word file", type="filepath")
58
 
59
  convert_pdf_to_word.click(pdf_to_word, inputs=[pdf_input], outputs=[word_output])
60
 
61
  with gr.Column():
62
  with gr.Accordion("Word to PDF"):
63
- word_input = gr.File(label="Upload Word")
64
  convert_word_to_pdf = gr.Button("Convert to PDF")
65
- pdf_output = gr.File(label="Download PDF file", type="filepath")
66
 
67
  convert_word_to_pdf.click(word_to_pdf, inputs=[word_input], outputs=[pdf_output])
68
 
69
- app.launch()
 
3
  from docx import Document
4
  from fpdf import FPDF
5
  import os
6
+ import tempfile
7
 
8
  title_and_description = """
9
+ # PDF to Word and Word to PDF Converter
10
+
11
+ This tool allows you to convert PDF files to Word documents and Word documents to PDF files.
12
  """
13
 
14
  def pdf_to_word(pdf_file):
15
+ """
16
+ Converts a PDF file to a Word document.
17
+ """
18
+ try:
19
+ # Create a temporary directory to store the output file
20
+ with tempfile.TemporaryDirectory() as temp_dir:
21
+ docx_filename = os.path.join(temp_dir, os.path.basename(pdf_file.name).replace('.pdf', '.docx'))
22
+
23
+ # Convert PDF to Word
24
+ cv = Converter(pdf_file.name)
25
+ cv.convert(docx_filename, multi_processing=True, start=0, end=None)
26
+ cv.close()
27
+
28
+ # Return the path to the converted file
29
+ return docx_filename
30
+ except Exception as e:
31
+ return f"Error: {e}"
32
 
33
  def word_to_pdf(docx_file):
34
+ """
35
+ Converts a Word document to a PDF file.
36
+ """
37
+ try:
38
+ # Create a temporary directory to store the output file
39
+ with tempfile.TemporaryDirectory() as temp_dir:
40
+ pdf_filename = os.path.join(temp_dir, "output.pdf")
41
+
42
+ # Convert Word to PDF
43
+ doc = Document(docx_file.name)
44
+ pdf = FPDF(format='A4')
45
+ pdf.set_auto_page_break(auto=True, margin=15)
46
+ pdf.add_page()
47
+ pdf.add_font('Arial', '', 'Arial.ttf', uni=True)
48
+ pdf.set_font('Arial', size=12)
49
 
50
+ for para in doc.paragraphs:
51
+ text = para.text.strip()
52
+ if not text:
53
+ continue
54
 
55
+ words = text.split()
56
+ line = ''
57
+ for word in words:
58
+ if pdf.get_string_width(line + word) < (pdf.w - 2 * pdf.l_margin):
59
+ line += word + ' '
60
+ else:
61
+ pdf.cell(0, 10, line, ln=True)
62
+ line = word + ' '
63
+ if line:
64
+ pdf.cell(0, 10, line, ln=True)
65
 
66
+ pdf.output(pdf_filename)
67
+
68
+ # Return the path to the converted file
69
+ return pdf_filename
70
+ except Exception as e:
71
+ return f"Error: {e}"
72
 
73
  with gr.Blocks() as app:
74
  gr.Markdown(title_and_description)
 
76
  with gr.Row():
77
  with gr.Column():
78
  with gr.Accordion("PDF to Word"):
79
+ pdf_input = gr.File(label="Upload PDF", file_types=[".pdf"])
80
  convert_pdf_to_word = gr.Button("Convert to Word")
81
+ word_output = gr.File(label="Download Word file", type="filepath", file_types=[".docx"])
82
 
83
  convert_pdf_to_word.click(pdf_to_word, inputs=[pdf_input], outputs=[word_output])
84
 
85
  with gr.Column():
86
  with gr.Accordion("Word to PDF"):
87
+ word_input = gr.File(label="Upload Word", file_types=[".docx"])
88
  convert_word_to_pdf = gr.Button("Convert to PDF")
89
+ pdf_output = gr.File(label="Download PDF file", type="filepath", file_types=[".pdf"])
90
 
91
  convert_word_to_pdf.click(word_to_pdf, inputs=[word_input], outputs=[pdf_output])
92
 
93
+ app.launch()