Spaces:
Sleeping
Sleeping
import streamlit as st | |
from pdf2docx import Converter | |
import tempfile | |
import os | |
def pdf_to_word(pdf_file, word_file): | |
# Create a Converter object | |
cv = Converter(pdf_file) | |
# Convert the PDF to a Word document | |
cv.convert(word_file, start=0, end=None) | |
# Close the Converter | |
cv.close() | |
# Streamlit app | |
st.title('PDF to Word Converter') | |
# Upload PDF file | |
uploaded_file = st.file_uploader("Choose a PDF file", type="pdf") | |
if uploaded_file is not None: | |
# Create a temporary file for the PDF | |
with tempfile.NamedTemporaryFile(delete=False) as temp_pdf: | |
temp_pdf.write(uploaded_file.read()) | |
temp_pdf_path = temp_pdf.name | |
# Create a temporary file for the Word document | |
temp_word_path = tempfile.mktemp(suffix=".docx") | |
# Convert PDF to Word | |
pdf_to_word(temp_pdf_path, temp_word_path) | |
# Provide a download link for the Word document | |
with open(temp_word_path, "rb") as f: | |
st.download_button( | |
label="Download Word Document", | |
data=f, | |
file_name="converted.docx", | |
mime="application/vnd.openxmlformats-officedocument.wordprocessingml.document" | |
) | |
# Clean up temporary files | |
os.remove(temp_pdf_path) | |
os.remove(temp_word_path) |