Spaces:
Sleeping
Sleeping
File size: 1,417 Bytes
8f78c8f |
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 |
import tempfile
import io
from typing import List
import pandas as pd
import base64
def generate_excel_base64(dataframe: pd.DataFrame) -> str:
"""Generates an Excel file from the provided data frame and returns it as a base64 string."""
output_stream = io.BytesIO() # Create in-memory buffer
# Ensure `xlsxwriter` writes to the buffer
with pd.ExcelWriter(output_stream, engine="xlsxwriter") as writer:
dataframe.to_excel(writer, index=False, sheet_name="Data")
output_stream.seek(0) # Move to the beginning for reading
base64_data = base64.b64encode(output_stream.getvalue()).decode(
"utf-8"
) # Encode to base64
return base64_data # Return base64 string directly
def generate_excel(dataframe: pd.DataFrame) -> str:
"""Generates an Excel file from the provided data frame."""
output_stream = io.BytesIO() # Create in-memory buffer
# Ensure `xlsxwriter` writes to the buffer
with pd.ExcelWriter(output_stream, engine="xlsxwriter") as writer:
dataframe.to_excel(writer, index=False, sheet_name="Data")
output_stream.seek(0) # Move to the beginning for reading
with tempfile.NamedTemporaryFile(delete=False, suffix=".xlsx") as tmp_file:
tmp_file.write(output_stream.getvalue()) # Write bytes to temp file
tmp_path = tmp_file.name # Get temp file path
return tmp_path # ✅ Return file path directly
|