prithivMLmods commited on
Commit
7809ae8
·
verified ·
1 Parent(s): 6f9b4f8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -54
app.py CHANGED
@@ -1,66 +1,69 @@
1
  import gradio as gr
2
- from reportlab.lib.pagesizes import letter
3
- from reportlab.pdfgen import canvas
4
  from docx import Document
 
5
  import os
6
 
7
- def word_to_pdf(word_file):
8
- try:
9
- doc = Document(word_file.name)
10
- pdf_file = "output.pdf"
11
- c = canvas.Canvas(pdf_file, pagesize=letter)
12
- width, height = letter
13
-
14
- for paragraph in doc.paragraphs:
15
- text = paragraph.text
16
- if text.strip():
17
- c.drawString(72, height - 72, text)
18
- height -= 14
19
- if height < 72: # Create a new page if space runs out
20
- c.showPage()
21
- height = letter[1]
22
-
23
- c.save()
24
- return pdf_file
25
- except Exception as e:
26
- return f"Error: {e}"
27
 
28
  def pdf_to_word(pdf_file):
29
- try:
30
- from PyPDF2 import PdfReader
31
-
32
- reader = PdfReader(pdf_file.name)
33
- doc = Document()
34
-
35
- for page in reader.pages:
36
- text = page.extract_text()
37
- if text.strip():
38
- doc.add_paragraph(text)
39
-
40
- word_file = "output.docx"
41
- doc.save(word_file)
42
- return word_file
43
- except Exception as e:
44
- return f"Error: {e}"
45
-
46
- def interface():
47
- with gr.Blocks() as app:
48
- gr.Markdown("## PDF to Word and Word to PDF Converter")
49
 
50
- with gr.Tab("Word to PDF"):
51
- word_input = gr.File(label="Upload Word File", file_types=[".docx"])
52
- pdf_output = gr.File(label="Download PDF File")
53
- word_to_pdf_button = gr.Button("Convert Word to PDF")
 
 
 
 
 
54
 
55
- with gr.Tab("PDF to Word"):
56
- pdf_input = gr.File(label="Upload PDF File", file_types=[".pdf"])
57
- word_output = gr.File(label="Download Word File")
58
- pdf_to_word_button = gr.Button("Convert PDF to Word")
 
 
 
 
 
 
 
 
 
 
 
59
 
60
- word_to_pdf_button.click(word_to_pdf, inputs=word_input, outputs=pdf_output)
61
- pdf_to_word_button.click(pdf_to_word, inputs=pdf_input, outputs=word_output)
62
 
63
- return app
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64
 
65
- app = interface()
66
  app.launch()
 
1
  import gradio as gr
2
+ 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 converter
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)
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: # Ignorar linhas vazias
33
+ continue
34
+ # Quebrar o texto em várias linhas se necessário
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)
51
+
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()