prithivMLmods commited on
Commit
1a078b5
·
verified ·
1 Parent(s): 2608258

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -59
app.py CHANGED
@@ -1,66 +1,66 @@
1
  import gradio as gr
2
- import fitz # PyMuPDF
3
- from docx import Document
4
- from reportlab.pdfgen import canvas
5
  from reportlab.lib.pagesizes import letter
6
- import io
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
 
8
- # PDF to Word conversion function
9
  def pdf_to_word(pdf_file):
10
- # Read PDF file using PyMuPDF
11
- doc = fitz.open(stream=pdf_file.read(), filetype="pdf")
12
- text = ""
13
- for page in doc:
14
- text += page.get_text()
15
-
16
- # Create a Word document using python-docx
17
- docx = Document()
18
- docx.add_paragraph(text)
19
-
20
- # Save the Word document to a bytes buffer
21
- buffer = io.BytesIO()
22
- docx.save(buffer)
23
- buffer.seek(0)
24
-
25
- return buffer, "converted.docx"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
 
27
- # Word to PDF conversion function
28
- def word_to_pdf(docx_file):
29
- # Read Word document using python-docx
30
- doc = Document(docx_file)
31
- text = ""
32
- for para in doc.paragraphs:
33
- text += para.text + "\n"
34
-
35
- # Create a PDF using reportlab
36
- buffer = io.BytesIO()
37
- c = canvas.Canvas(buffer, pagesize=letter)
38
- textobject = c.beginText()
39
- textobject.setTextOrigin(50, 750)
40
- lines = text.split('\n')
41
- for line in lines:
42
- textobject.textLine(line)
43
- c.drawText(textobject)
44
- c.showPage()
45
- c.save()
46
- buffer.seek(0)
47
-
48
- return buffer, "converted.pdf"
49
 
50
- # Gradio interface
51
- with gr.Blocks() as demo:
52
- gr.Markdown("<h1>PDF to Word and Word to PDF Converter</h1>")
53
-
54
- with gr.Tab("PDF to Word"):
55
- pdf_input = gr.File(label="Upload PDF File", type="file")
56
- pdf_convert_btn = gr.Button("Convert to Word")
57
- word_output = gr.File(label="Download Word File")
58
- pdf_convert_btn.click(pdf_to_word, inputs=pdf_input, outputs=word_output)
59
-
60
- with gr.Tab("Word to PDF"):
61
- word_input = gr.File(label="Upload Word File", type="file")
62
- word_convert_btn = gr.Button("Convert to PDF")
63
- pdf_output = gr.File(label="Download PDF File")
64
- word_convert_btn.click(word_to_pdf, inputs=word_input, outputs=pdf_output)
65
 
66
- demo.launch()
 
 
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()