Below is the program on how to create a dynamic JSON file from reading data through an excel file as input for json in python.
First, install openpyxl from the following command to read the data from an excel file in python
pip install openpyxl
import os
import openpyxl
import json
# create a JSON file to write dynamic JSON data.
f = open("C:\\Users\\My_Dyanamic_JSON_Test".json", "a")
# Open excel file for input
path = "C:\\Users\\Excel_input_File.xlsx"
# create workbook object for excel file
wb_obj = openpyxl.load_workbook(path)
# create a workbook sheet object for the excel file
sheet_obj = wb_obj["Sheet1"]
# get the data from the workbook sheet object from an excel file
Row1_cell1 = sheet_obj["A" + str(1)]
Row1_cell2 = sheet_obj["B" + str(1)]
Row1_cell3 = sheet_obj["C" + str(1)]
Row1_cell4 = sheet_obj["D" + str(1)]
Row1_cell5 = sheet_obj["E" + str(1)]
Row1_cell6 = sheet_obj["F" + str(1)]
# define a payload
payload = {
'Key1': Row1_cell1.value,
'Key1': Row1_cell2.value,
'Key1': Row1_cell3.value,
'Key1': Row1_cell4.value,
'Key1': Row1_cell5.value,
'Key1': Row1_cell6.value,
'Key1': 10,
'Key1': 20
}
# dump the payload
json_object = json.dumps(payload, indent=4)
# write the payload to JSON file
f.write(json_object)
Comments
Post a Comment