Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| from pdf2docx import Converter | |
| import tempfile | |
| import os | |
| st.set_page_config(page_title="PDF to Word Converter", page_icon="📄") | |
| st.title("📄 PDF to Word Converter") | |
| uploaded_file = st.file_uploader("Upload PDF file", type=["pdf"]) | |
| def convert_pdf_to_word(pdf_path, docx_path): | |
| cv = Converter(pdf_path) | |
| cv.convert(docx_path, start=0, end=None) | |
| cv.close() | |
| if uploaded_file: | |
| st.success("PDF uploaded successfully!") | |
| if st.button("Convert to Word"): | |
| with tempfile.TemporaryDirectory() as tmpdir: | |
| pdf_path = os.path.join(tmpdir, "input.pdf") | |
| docx_path = os.path.join(tmpdir, "output.docx") | |
| # Save uploaded PDF | |
| with open(pdf_path, "wb") as f: | |
| f.write(uploaded_file.read()) | |
| # Convert | |
| convert_pdf_to_word(pdf_path, docx_path) | |
| # Read result | |
| with open(docx_path, "rb") as f: | |
| doc_bytes = f.read() | |
| st.success("Conversion completed!") | |
| st.download_button( | |
| "⬇️ Download Word File", | |
| data=doc_bytes, | |
| file_name="converted.docx", | |
| mime="application/vnd.openxmlformats-officedocument.wordprocessingml.document" | |
| ) | |
| st.markdown("---") | |
| st.caption("Built with ❤️ using Streamlit + pdf2docx") |