Skip to content

Instantly share code, notes, and snippets.

@patmandenver
Created February 7, 2020 01:14
Show Gist options
  • Save patmandenver/92e22a2772befc57ba858333175433ce to your computer and use it in GitHub Desktop.
Save patmandenver/92e22a2772befc57ba858333175433ce to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import openpyxl
import json
from openpyxl import Workbook
def populate_sheet(json_data, sheet):
sheet.cell(1,1, "Month")
sheet.cell(1,2, "food")
sheet.cell(1,3, "heating")
sheet.cell(1,4, "rent")
row = 1
for month in json_data.keys():
row+=1
sheet.cell(row,1,month)
sheet.cell(row,2,float(json_data[month]["food"]))
sheet.cell(row,3,float(json_data[month]["heating"]))
sheet.cell(row,4,float(json_data[month]["rent"]))
#############################################
# MAIN
#############################################
if __name__ == '__main__':
json_data = {}
with open("original.json") as json_file:
json_data = json.load(json_file)
wb = Workbook()
#When you make a new workbook you get a new blank active sheet
#We need to delete it since we do not want it
wb.remove(wb.active)
for year in json_data.keys():
sheet = wb.create_sheet(title=year)
populate_sheet(json_data[year], sheet)
#Save it to excel
wb.save("test_json.xlsx")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment