How to write data from JSON file to excel file by openpyxl?
Question:
I have a json
file with data that I would like to save to an xlsx
file. I used the openpyxl
library and stopped at the stage of creating a for
loop to save each user in turn in a separate line and his data in separate cells.
json file:
{
"entries": [
{
"attributes": {
"cn": "Bruce Wayne",
"displayName": "Batman",
"distinguishedName": "CN=Bruce Wayne,OU=Users,OU=DC-COMICS,DC=universum,DC=local",
"primaryGroupID": 513,
"sAMAccountName": "batman",
"sAMAccountType": 805306368,
"userAccountControl": 514,
"whenCreated": "2016-04-19 10:06:25+00:00"
},
"dn": "CN=Bruce Wayne,OU=Users,OU=DC-COMICS,DC=universum,DC=local"
},
{
"attributes": {
"cn": "Clark Kent",
"displayName": "Superman",
"distinguishedName": "CN=Clark Kent,OU=Users,OU=DC-COMICS,DC=universum,DC=local",
"primaryGroupID": 513,
"sAMAccountName": "superman",
"sAMAccountType": 805306368,
"userAccountControl": 514,
"whenCreated": "2016-04-19 10:06:25+00:00"
},
"dn": "CN=Clark Kent,OU=Users,OU=DC-COMICS,DC=universum,DC=local"
}
]
}
My code:
import json
import configparser
### load main_config.ini file
main_config_file = './config/main_config.ini' # sciezka do pliku main_config.ini z
ustawieniami
main_config = configparser.RawConfigParser()
main_config.read(main_config_file)
### VARIABLES
# from configs files
root_path = main_config['GLOBAL']['root_path']
json_dir = main_config['GLOBAL']['json_dir']
encoded_retrived_users_file = main_config['USERS']['encoded_users_file_name']
# load data from JSON file
encoded_retrived_users = './JSON/test.json'
#encoded_retrived_users = root_path + json_dir + encoded_retrived_users_file # sciezka
do pliku encoded_users_from_LDAP.json
with open(encoded_retrived_users, 'r', encoding="UTF-8") as file:
data = json.load(file)
retrived_users = data['entries']
### CONSOLE VIEW
print("----------------------------------")
for user in retrived_users:
no = str(i)
attributes = user['attributes']
cn = attributes['cn']
sAMAccountName = attributes['sAMAccountName']
i += i
print("No: " + no)
print("cn: " + cn)
print("sAMAccountName: " + sAMAccountName)
print("----------------------------------")
### EXCEL
import openpyxl
from openpyxl import Workbook
wb = Workbook()
# Grab the active worksheet
ws_01 = wb.active
# Set the title of the worksheet
ws_01.title = "llogon>30d"
# Set first row
ws_01.cell(1, 1, "cn")
ws_01.cell(1, 2, "sAMA")
# Save it in an Excel file
wb.save("./test/test.xlsx")
What I would like to have:
Answers:
By the time import openpyxl
is called, retrived_users
is an array of objects, not JSON. The same code that writes to the console can be used to write to an Excel file :
wb = Workbook()
ws_01 = wb.active
ws_01.title = "llogon>30d"
i=0
for user in retrived_users:
no = str(i)
attributes = user['attributes']
cn = attributes['cn']
sAMAccountName = attributes['sAMAccountName']
ws_01.cell(i, 1, cn)
ws_01.cell(i, 2, sAMAccountName)
i += i
wb.save("./test/test.xlsx")
It’s certainly possible to write more elegant code, but this should work
An option using pandas:
import pandas as pd
import json
with open('test.json') as f:
j = json.load(f)
df = pd.DataFrame({
k:[i['attributes'][k] for i in j['entries']]
for k in j['entries'][0]['attributes'].keys()
})
df.whenCreated = pd.to_datetime(df.whenCreated).dt.tz_localize(None)
df.to_excel('test.xlsx')
Output:
I have a json
file with data that I would like to save to an xlsx
file. I used the openpyxl
library and stopped at the stage of creating a for
loop to save each user in turn in a separate line and his data in separate cells.
json file:
{
"entries": [
{
"attributes": {
"cn": "Bruce Wayne",
"displayName": "Batman",
"distinguishedName": "CN=Bruce Wayne,OU=Users,OU=DC-COMICS,DC=universum,DC=local",
"primaryGroupID": 513,
"sAMAccountName": "batman",
"sAMAccountType": 805306368,
"userAccountControl": 514,
"whenCreated": "2016-04-19 10:06:25+00:00"
},
"dn": "CN=Bruce Wayne,OU=Users,OU=DC-COMICS,DC=universum,DC=local"
},
{
"attributes": {
"cn": "Clark Kent",
"displayName": "Superman",
"distinguishedName": "CN=Clark Kent,OU=Users,OU=DC-COMICS,DC=universum,DC=local",
"primaryGroupID": 513,
"sAMAccountName": "superman",
"sAMAccountType": 805306368,
"userAccountControl": 514,
"whenCreated": "2016-04-19 10:06:25+00:00"
},
"dn": "CN=Clark Kent,OU=Users,OU=DC-COMICS,DC=universum,DC=local"
}
]
}
My code:
import json
import configparser
### load main_config.ini file
main_config_file = './config/main_config.ini' # sciezka do pliku main_config.ini z
ustawieniami
main_config = configparser.RawConfigParser()
main_config.read(main_config_file)
### VARIABLES
# from configs files
root_path = main_config['GLOBAL']['root_path']
json_dir = main_config['GLOBAL']['json_dir']
encoded_retrived_users_file = main_config['USERS']['encoded_users_file_name']
# load data from JSON file
encoded_retrived_users = './JSON/test.json'
#encoded_retrived_users = root_path + json_dir + encoded_retrived_users_file # sciezka
do pliku encoded_users_from_LDAP.json
with open(encoded_retrived_users, 'r', encoding="UTF-8") as file:
data = json.load(file)
retrived_users = data['entries']
### CONSOLE VIEW
print("----------------------------------")
for user in retrived_users:
no = str(i)
attributes = user['attributes']
cn = attributes['cn']
sAMAccountName = attributes['sAMAccountName']
i += i
print("No: " + no)
print("cn: " + cn)
print("sAMAccountName: " + sAMAccountName)
print("----------------------------------")
### EXCEL
import openpyxl
from openpyxl import Workbook
wb = Workbook()
# Grab the active worksheet
ws_01 = wb.active
# Set the title of the worksheet
ws_01.title = "llogon>30d"
# Set first row
ws_01.cell(1, 1, "cn")
ws_01.cell(1, 2, "sAMA")
# Save it in an Excel file
wb.save("./test/test.xlsx")
What I would like to have:
By the time import openpyxl
is called, retrived_users
is an array of objects, not JSON. The same code that writes to the console can be used to write to an Excel file :
wb = Workbook()
ws_01 = wb.active
ws_01.title = "llogon>30d"
i=0
for user in retrived_users:
no = str(i)
attributes = user['attributes']
cn = attributes['cn']
sAMAccountName = attributes['sAMAccountName']
ws_01.cell(i, 1, cn)
ws_01.cell(i, 2, sAMAccountName)
i += i
wb.save("./test/test.xlsx")
It’s certainly possible to write more elegant code, but this should work
An option using pandas:
import pandas as pd
import json
with open('test.json') as f:
j = json.load(f)
df = pd.DataFrame({
k:[i['attributes'][k] for i in j['entries']]
for k in j['entries'][0]['attributes'].keys()
})
df.whenCreated = pd.to_datetime(df.whenCreated).dt.tz_localize(None)
df.to_excel('test.xlsx')