Created
February 8, 2020 15:15
-
-
Save patmandenver/8ee9a044164ad3a64eafb32043b6c025 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import openpyxl | |
import json | |
from openpyxl import Workbook | |
from openpyxl.styles import numbers | |
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"])) | |
cell.number_format = numbers.FORMAT_PERCENTAGE | |
############################################# | |
# 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("formatted.xlsx") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment