Edits history of script submission #6066 for ' Convert CSV file to Excel (windmill)'

  • python3
    import base64
    from io import BytesIO
    import pandas as pd
    from io import StringIO
    import openpyxl
    
    def main(csv_bytes: bytes) -> str:
        # Convert bytes to string
        csv_string = csv_bytes.decode('utf-8')
        
        # Use StringIO to convert string to a file-like object for reading into DataFrame
        csv_file = StringIO(csv_string)
        
        # Read CSV data into DataFrame
        df = pd.read_csv(csv_file)
        
        # Convert DataFrame to Excel and save to a BytesIO object
        excel_buffer = BytesIO()
        df.to_excel(excel_buffer, index=False)
        excel_buffer.seek(0)  # Rewind the buffer to the beginning
        
        # Encode the Excel file into a base64 string
        base64_excel = base64.b64encode(excel_buffer.read()).decode('utf-8')
    
        return { "file": { "content": base64_excel, "filename": "data.xlsx" } }

    Submitted by henri186 771 days ago