Skip to content

Instantly share code, notes, and snippets.

@patmandenver
Created February 11, 2020 00:19
Show Gist options
  • Select an option

  • Save patmandenver/4f424f72a6a001c6f51f83796af0259f to your computer and use it in GitHub Desktop.

Select an option

Save patmandenver/4f424f72a6a001c6f51f83796af0259f 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, ws):
ws.cell(1,1, "Month")
ws.cell(1,2, "food")
ws.cell(1,3, "heating")
ws.cell(1,4, "rent")
row = 1
for month in json_data.keys():
row+=1
ws.cell(row,1,month)
cell = ws.cell(row,2,float(json_data[month]["food"]))
cell = ws.cell(row,3,float(json_data[month]["heating"]))
cell = ws.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