Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,11 +1,11 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
|
|
|
|
| 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
|
| 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
|
| 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 |
-
#
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
# Convert
|
| 47 |
-
cv = Converter(temp_pdf_path)
|
| 48 |
-
cv.convert(temp_docx_path, start=0, end=None)
|
| 49 |
-
cv.close()
|
| 50 |
|
| 51 |
-
#
|
| 52 |
-
|
| 53 |
-
|
|
|
|
| 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=
|
| 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 |
+
|