Danial7 commited on
Commit
5e756c1
·
verified ·
1 Parent(s): aa0aeaf

Create app.py

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