AzizWazir commited on
Commit
9a256f9
·
verified ·
1 Parent(s): 3374972

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -0
app.py ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from PyPDF2 import PdfReader
3
+ from docx import Document
4
+ from io import BytesIO
5
+
6
+ def pdf_to_word(pdf_file):
7
+ """Convert a PDF file to a Word file."""
8
+ reader = PdfReader(pdf_file)
9
+ document = Document()
10
+
11
+ for page in reader.pages:
12
+ text = page.extract_text()
13
+ document.add_paragraph(text)
14
+
15
+ word_file = BytesIO()
16
+ document.save(word_file)
17
+ word_file.seek(0)
18
+ return word_file
19
+
20
+ # Streamlit app configuration
21
+ st.set_page_config(page_title="PDF to Word Converter", page_icon="🖋", layout="centered")
22
+
23
+ # App header
24
+ st.title("PDF to Word Converter")
25
+ st.write("Upload a PDF file, and we will convert it into a Word document for you.")
26
+
27
+ # File uploader
28
+ uploaded_file = st.file_uploader("Choose a PDF file", type="pdf")
29
+
30
+ if uploaded_file is not None:
31
+ with st.spinner("Converting PDF to Word..."):
32
+ try:
33
+ word_file = pdf_to_word(uploaded_file)
34
+ st.success("Conversion successful!")
35
+ st.download_button(
36
+ label="Download Word file",
37
+ data=word_file,
38
+ file_name="converted.docx",
39
+ mime="application/vnd.openxmlformats-officedocument.wordprocessingml.document"
40
+ )
41
+ except Exception as e:
42
+ st.error(f"An error occurred: {str(e)}")