Created
February 8, 2020 17:21
-
-
Save patmandenver/cfbb2425fc73fecb09d349d428c770a1 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 | |
fmt_acct = u'_($* #,##0.00_);[Red]_($* (#,##0.00);_($* -_0_0_);_(@' | |
for month in json_data.keys(): | |
row+=1 | |
ws.cell(row,1,month) | |
cell = ws.cell(row,2,float(json_data[month]["food"])) | |
cell.number_format = fmt_acct | |
cell = ws.cell(row,3,float(json_data[month]["heating"])) | |
cell.number_format = fmt_acct | |
cell = ws.cell(row,4,float(json_data[month]["rent"])) | |
cell.number_format = fmt_acct | |
############################################# | |
# 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