srikanththirumani commited on
Commit
c58fabf
·
verified ·
1 Parent(s): cd8fe0b

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -0
app.py ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from pdf2docx import Converter
3
+ import tempfile
4
+ import os
5
+
6
+ def pdf_to_word(pdf_file, word_file):
7
+ # Create a Converter object
8
+ cv = Converter(pdf_file)
9
+ # Convert the PDF to a Word document
10
+ cv.convert(word_file, start=0, end=None)
11
+ # Close the Converter
12
+ cv.close()
13
+
14
+ # Streamlit app
15
+ st.title('PDF to Word Converter')
16
+
17
+ # Upload PDF file
18
+ uploaded_file = st.file_uploader("Choose a PDF file", type="pdf")
19
+
20
+ if uploaded_file is not None:
21
+ # Create a temporary file for the PDF
22
+ with tempfile.NamedTemporaryFile(delete=False) as temp_pdf:
23
+ temp_pdf.write(uploaded_file.read())
24
+ temp_pdf_path = temp_pdf.name
25
+
26
+ # Create a temporary file for the Word document
27
+ temp_word_path = tempfile.mktemp(suffix=".docx")
28
+
29
+ # Convert PDF to Word
30
+ pdf_to_word(temp_pdf_path, temp_word_path)
31
+
32
+ # Provide a download link for the Word document
33
+ with open(temp_word_path, "rb") as f:
34
+ st.download_button(
35
+ label="Download Word Document",
36
+ data=f,
37
+ file_name="converted.docx",
38
+ mime="application/vnd.openxmlformats-officedocument.wordprocessingml.document"
39
+ )
40
+
41
+ # Clean up temporary files
42
+ os.remove(temp_pdf_path)
43
+ os.remove(temp_word_path)