SafiUllahAdam commited on
Commit
8ca5074
Β·
verified Β·
1 Parent(s): 5978f1f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -25
app.py CHANGED
@@ -1,11 +1,11 @@
1
  import streamlit as st
2
- from pdf2docx import Converter
 
3
  from io import BytesIO
4
- import tempfile
5
 
6
  st.set_page_config(page_title="PDF to Word Converter", page_icon="πŸ“„βž‘οΈπŸ“", layout="centered")
7
 
8
- # Custom CSS for modern look
9
  st.markdown("""
10
  <style>
11
  .main {
@@ -19,44 +19,37 @@ st.markdown("""
19
  margin: 10px 0px;
20
  font-size: 16px;
21
  }
22
- .stFileUploader {
23
- margin-bottom: 20px;
24
- }
25
  </style>
26
  """, unsafe_allow_html=True)
27
 
28
  # Title
29
  st.title("πŸ“„ ➑️ πŸ“ PDF to Word Converter")
30
 
31
- st.write("Upload your PDF file and download the converted Word (.docx) file.")
32
 
33
- # File uploader
34
  uploaded_file = st.file_uploader("Choose a PDF file", type="pdf")
35
 
36
  if uploaded_file:
37
  if st.button("Convert to Word"):
38
- # Create a temporary file for the uploaded PDF
39
- with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as temp_pdf:
40
- temp_pdf.write(uploaded_file.read())
41
- temp_pdf_path = temp_pdf.name
42
-
43
- # Create a temporary file for the output Word document
44
- temp_docx_path = temp_pdf_path.replace(".pdf", ".docx")
45
-
46
- # Convert
47
- cv = Converter(temp_pdf_path)
48
- cv.convert(temp_docx_path, start=0, end=None)
49
- cv.close()
50
 
51
- # Read the converted Word file into memory
52
- with open(temp_docx_path, "rb") as f:
53
- word_data = f.read()
 
54
 
55
- # Offer download
56
  st.success("Conversion successful! Download your Word file below.")
57
  st.download_button(
58
  label="πŸ“₯ Download Word file",
59
- data=word_data,
60
  file_name="converted.docx",
61
  mime="application/vnd.openxmlformats-officedocument.wordprocessingml.document"
62
  )
 
 
1
  import streamlit as st
2
+ import fitz # PyMuPDF
3
+ from docx import Document
4
  from io import BytesIO
 
5
 
6
  st.set_page_config(page_title="PDF to Word Converter", page_icon="πŸ“„βž‘οΈπŸ“", layout="centered")
7
 
8
+ # Custom CSS
9
  st.markdown("""
10
  <style>
11
  .main {
 
19
  margin: 10px 0px;
20
  font-size: 16px;
21
  }
 
 
 
22
  </style>
23
  """, unsafe_allow_html=True)
24
 
25
  # Title
26
  st.title("πŸ“„ ➑️ πŸ“ PDF to Word Converter")
27
 
28
+ st.write("Upload your PDF file and get a Word (.docx) version with extracted text.")
29
 
 
30
  uploaded_file = st.file_uploader("Choose a PDF file", type="pdf")
31
 
32
  if uploaded_file:
33
  if st.button("Convert to Word"):
34
+ # Load PDF
35
+ pdf_document = fitz.open(stream=uploaded_file.read(), filetype="pdf")
36
+
37
+ # Create Word Document
38
+ doc = Document()
39
+ for page in pdf_document:
40
+ text = page.get_text()
41
+ doc.add_paragraph(text)
 
 
 
 
42
 
43
+ # Save Word file to memory
44
+ word_file = BytesIO()
45
+ doc.save(word_file)
46
+ word_file.seek(0)
47
 
 
48
  st.success("Conversion successful! Download your Word file below.")
49
  st.download_button(
50
  label="πŸ“₯ Download Word file",
51
+ data=word_file,
52
  file_name="converted.docx",
53
  mime="application/vnd.openxmlformats-officedocument.wordprocessingml.document"
54
  )
55
+