Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
+
from fpdf import FPDF
|
| 4 |
+
|
| 5 |
+
def excel_to_pdf(file):
|
| 6 |
+
# Load Excel file
|
| 7 |
+
df = pd.read_excel(file.name)
|
| 8 |
+
|
| 9 |
+
# Create PDF object
|
| 10 |
+
pdf = FPDF()
|
| 11 |
+
pdf.add_page()
|
| 12 |
+
|
| 13 |
+
# Add table headers
|
| 14 |
+
pdf.set_font("Arial", size=12)
|
| 15 |
+
col_width = pdf.w / (len(df.columns) + 1)
|
| 16 |
+
row_height = pdf.font_size
|
| 17 |
+
|
| 18 |
+
for col in df.columns:
|
| 19 |
+
pdf.cell(col_width, row_height, col, border=1)
|
| 20 |
+
pdf.ln(row_height)
|
| 21 |
+
|
| 22 |
+
# Add table rows
|
| 23 |
+
for row in df.itertuples(index=False):
|
| 24 |
+
for item in row:
|
| 25 |
+
pdf.cell(col_width, row_height, str(item), border=1)
|
| 26 |
+
pdf.ln(row_height)
|
| 27 |
+
|
| 28 |
+
# Save PDF to a file
|
| 29 |
+
output_pdf = "output.pdf"
|
| 30 |
+
pdf.output(output_pdf)
|
| 31 |
+
|
| 32 |
+
return output_pdf
|
| 33 |
+
|
| 34 |
+
# Gradio Interface
|
| 35 |
+
iface = gr.Interface(
|
| 36 |
+
fn=excel_to_pdf,
|
| 37 |
+
inputs=gr.inputs.File(label="Upload Excel File", type="file"),
|
| 38 |
+
outputs=gr.outputs.File(label="Download PDF File")
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
if __name__ == "__main__":
|
| 42 |
+
iface.launch()
|