Spaces:
Running
Running
| import streamlit as st | |
| from pdf2image import convert_from_bytes | |
| from PIL import Image | |
| import io | |
| import fitz # PyMuPDF for PDF handling | |
| import docx | |
| from fpdf import FPDF | |
| import os | |
| os.system("apt-get update && apt-get install -y poppler-utils") | |
| st.set_page_config(page_title="PDF & Image Converter", page_icon="π", layout="centered") | |
| st.title("π PDF & Image Converter") | |
| st.markdown("Convert PDFs to images, Word, and vice versa.") | |
| # File uploader | |
| uploaded_file = st.file_uploader("Upload a file (PDF, PNG, JPG, JPEG, DOCX)", type=["pdf", "png", "jpg", "jpeg", "docx"]) | |
| if uploaded_file: | |
| file_type = uploaded_file.type | |
| # ---------------- PDF TO IMAGE ---------------- | |
| if file_type == "application/pdf": | |
| st.subheader("Convert PDF to Image") | |
| images = convert_from_bytes(uploaded_file.read()) | |
| format_option = st.selectbox("Select image format", ["PNG", "JPEG", "JPG"]) | |
| quality = st.slider("Image Quality", 50, 100, 90) | |
| zip_buffer = io.BytesIO() | |
| with st.spinner("Converting PDF to images..."): | |
| for i, img in enumerate(images): | |
| img_format = format_option.lower() | |
| img_byte_arr = io.BytesIO() | |
| img.save(img_byte_arr, format=img_format.upper(), quality=quality) | |
| st.image(img, caption=f"Page {i+1}", use_column_width=True) | |
| st.download_button(f"Download Page {i+1}", img_byte_arr.getvalue(), file_name=f"page_{i+1}.{img_format}") | |
| # ---------------- PDF TO WORD ---------------- | |
| st.subheader("Convert PDF to Word") | |
| if st.button("Convert to DOCX"): | |
| doc = docx.Document() | |
| pdf_reader = fitz.open(stream=uploaded_file.read(), filetype="pdf") | |
| for page in pdf_reader: | |
| text = page.get_text() | |
| doc.add_paragraph(text) | |
| word_buffer = io.BytesIO() | |
| doc.save(word_buffer) | |
| st.download_button("Download DOCX", word_buffer.getvalue(), file_name="converted.docx", mime="application/vnd.openxmlformats-officedocument.wordprocessingml.document") | |
| # ---------------- IMAGE TO PDF ---------------- | |
| elif file_type in ["image/png", "image/jpeg", "image/jpg"]: | |
| st.subheader("Convert Image to PDF") | |
| image = Image.open(uploaded_file) | |
| img_pdf_buffer = io.BytesIO() | |
| image.convert("RGB").save(img_pdf_buffer, format="PDF") | |
| st.download_button("Download PDF", img_pdf_buffer.getvalue(), file_name="converted.pdf", mime="application/pdf") | |
| # ---------------- WORD TO PDF ---------------- | |
| elif file_type == "application/vnd.openxmlformats-officedocument.wordprocessingml.document": | |
| st.subheader("Convert Word to PDF") | |
| if st.button("Convert to PDF"): | |
| doc = docx.Document(uploaded_file) | |
| pdf = FPDF() | |
| pdf.set_auto_page_break(auto=True, margin=10) | |
| pdf.add_page() | |
| pdf.set_font("Arial", size=12) | |
| for para in doc.paragraphs: | |
| pdf.cell(200, 10, txt=para.text, ln=True) | |
| pdf_buffer = io.BytesIO() | |
| pdf.output(pdf_buffer, "F") | |
| st.download_button("Download PDF", pdf_buffer.getvalue(), file_name="converted.pdf", mime="application/pdf") | |
| st.markdown("---") | |
| st.markdown("π‘ Developed with β€οΈ using Streamlit, PyMuPDF, pdf2image, and PIL") | |