Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Last active August 1, 2023 09:10
Show Gist options
  • Save aspose-com-gists/02b2579f7a9876d6142bf25b411ede1a to your computer and use it in GitHub Desktop.
Save aspose-com-gists/02b2579f7a9876d6142bf25b411ede1a to your computer and use it in GitHub Desktop.
Convert JSON to Excel Worksheet in Python
jsonInput = '[{"nodeId":1,"reputation":1134},{"nodeId":2,"reputation":547},{"nodeId":3,"reputation":1703},{"nodeId":4,"reputation":-199},{"nodeId":5,"reputation":-306},{"nodeId":6,"reputation":-49},{"nodeId":7,"reputation":1527},{"nodeId":8,"reputation":1223}]'
# create a blank Workbook object
workbook = Workbook()
# access default empty worksheet
worksheet = workbook.getWorksheets().get(0)
# set Styles
factory = CellsFactory()
style = factory.createStyle()
style.getFont().setBold(True)
style.getFont().setColor(Color.getBlueViolet())
# set JsonLayoutOptions for formatting
layoutOptions = JsonLayoutOptions()
layoutOptions.setArrayAsTable(True)
layoutOptions.setTitleStyle(style)
# import JSON data to default worksheet starting at cell A1
JsonUtility.importData(jsonInput, worksheet.getCells(), 0, 0, layoutOptions)
# save resultant file in JSON-TO-XLS format
workbook.save("output.xlsx", SaveFormat.AUTO);
jsonInput = '[{"nodeId":1,"reputation":1134},{"nodeId":2,"reputation":547},{"nodeId":3,"reputation":1703},{"nodeId":4,"reputation":-199},{"nodeId":5,"reputation":-306},{"nodeId":6,"reputation":-49},{"nodeId":7,"reputation":1527},{"nodeId":8,"reputation":1223}]'
# create a blank Workbook object
workbook = Workbook()
# access default empty worksheet
worksheet = workbook.getWorksheets().get(0)
# set JsonLayoutOptions for formatting
layoutOptions = JsonLayoutOptions()
layoutOptions.setArrayAsTable(True)
# import JSON data to default worksheet starting at cell A1
JsonUtility.importData(jsonInput, worksheet.getCells(), 0, 0, layoutOptions)
# save resultant file in JSON-TO-XLS format
workbook.save("output.xls", SaveFormat.AUTO)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment