Skip to content

Instantly share code, notes, and snippets.

@manutheblacker
Created January 2, 2024 04:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save manutheblacker/6eec47115d9ee965830ff85c47adf71c to your computer and use it in GitHub Desktop.
Save manutheblacker/6eec47115d9ee965830ff85c47adf71c to your computer and use it in GitHub Desktop.
How to convert a Excel file into Json using Python Openpyxl ?
import openpyxl # pip install openpyxl
from var_dump import var_dump # pip install var_dump , this method has the same behaviour like the PHP ones.
import json
import datetime
now = datetime.datetime.now()
path = "./atarada.xlsx" # path to your xlsx file
wb_obj = openpyxl.load_workbook( path )
sheet_obj = wb_obj.active
headers = {} # store xlsx file headers
content = [] # store xlsx file rows
for idx, rows in enumerate(sheet_obj):
ctt = {}
row_in_sheets = len( rows )
if idx == 0 :
for idt, vl in enumerate(rows):
headers[idt] = vl.value
else:
for idx,vl in enumerate(rows):
vlcv = vl.value
if isinstance(vl.value, datetime.datetime):
vlcv = 0
ctt[headers[idx]] = vlcv
content.append(ctt)
with open('product.json', 'w') as f:
json.dump(content, f) # store the result into this json file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment