Spaces:
Sleeping
Sleeping
File size: 2,041 Bytes
5e756c1 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
import streamlit as st
from io import BytesIO
from docx import Document
from pptx import Presentation
import pandas as pd
from fpdf import FPDF
st.title("📄 Office to PDF Converter")
uploaded_file = st.file_uploader("Upload a .docx, .xlsx, or .pptx file", type=["docx", "xlsx", "pptx"])
def convert_docx_to_pdf(file):
doc = Document(file)
pdf = FPDF()
pdf.add_page()
pdf.set_auto_page_break(auto=True, margin=15)
pdf.set_font("Arial", size=12)
for para in doc.paragraphs:
pdf.multi_cell(0, 10, para.text)
return pdf.output(dest='S').encode('latin1')
def convert_xlsx_to_pdf(file):
df = pd.read_excel(file)
pdf = FPDF()
pdf.add_page()
pdf.set_font("Arial", size=10)
col_width = pdf.w / (len(df.columns) + 1)
row_height = 8
for col in df.columns:
pdf.cell(col_width, row_height, col, border=1)
pdf.ln(row_height)
for _, row in df.iterrows():
for item in row:
pdf.cell(col_width, row_height, str(item), border=1)
pdf.ln(row_height)
return pdf.output(dest='S').encode('latin1')
def convert_pptx_to_pdf(file):
prs = Presentation(file)
pdf = FPDF()
pdf.set_font("Arial", size=14)
for slide in prs.slides:
pdf.add_page()
for shape in slide.shapes:
if hasattr(shape, "text"):
pdf.multi_cell(0, 10, shape.text)
return pdf.output(dest='S').encode('latin1')
if uploaded_file:
file_type = uploaded_file.name.split('.')[-1]
pdf_bytes = None
if file_type == 'docx':
pdf_bytes = convert_docx_to_pdf(uploaded_file)
elif file_type == 'xlsx':
pdf_bytes = convert_xlsx_to_pdf(uploaded_file)
elif file_type == 'pptx':
pdf_bytes = convert_pptx_to_pdf(uploaded_file)
if pdf_bytes:
st.success("✅ Conversion successful!")
st.download_button("📥 Download PDF", data=pdf_bytes, file_name="converted.pdf", mime="application/pdf")
else:
st.error("❌ Unsupported file type or conversion failed.")
|