Generate Excel Workbook from JSON Data
Incoming JSON Data to Python Operations
{
"records": [
{
"category": "Category A",
"value": 100
},
{
"category": "Category B",
"value": 200
}
]
}
Python Operations
import pandas as pd
from io import BytesIO
import base64
Responsedata = []
# Read incoming JSON data
data = pycode_data.get("records", [])
# Convert JSON data into a DataFrame
df = pd.DataFrame(data)
# Create an in-memory Excel file
output = BytesIO()
# Write data into an Excel worksheet
with pd.ExcelWriter(output, engine="openpyxl") as writer:
df.to_excel(writer, sheet_name="Data", index=False)
# Move pointer to the beginning of the file
output.seek(0)
# Encode file content into Base64
file_content = base64.b64encode(output.read()).decode()
# Prepare output response
response = {
"file_name": "Excel_Tab_Data.xlsx",
"file_content": file_content,
"mime_type": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
}
# Send data to output
Responsedata.append(response)
Outgoing data from Python Operation
{
"file_name": "Excel_Tab_Data.xlsx",
"file_content": "<Base64 Encoded Content>",
"mime_type": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
}