How to sorted data in xlsx column by openpyxl?
Question:
I would like to save sorted data from A to Z by column C in my Excel file.
My code:
### EXCEL
# Set column names
A = 'SURNAME'
B = 'NAME'
C = 'sAMAccountName'
# Set worksheet
wb = Workbook() # create excel worksheet
ws_01 = wb.active # Grab the active worksheet
ws_01.title = "all inf" # Set the title of the worksheet
# Set first row
title_row = 1
ws_01.cell(title_row, 1, A) # cell(row, col, value)
ws_01.cell(title_row, 2, B)
ws_01.cell(title_row, 3, C)
data_row = 2
for user in retrieved_users:
attributes = user['attributes']
sAMAccountName = attributes['sAMAccountName']
if(user_validation(sAMAccountName) == True):
A = attributes['sn']
B = attributes['givenName']
C = sAMAccountName
ws_01.cell(data_row, 1, str(A))
ws_01.cell(data_row, 2, str(B))
ws_01.cell(data_row, 3, str(C))
data_row = data_row + 1
# Save it in an Excel file
decoded_users_all_inf = root_path + reports_dir + users_all_inf_excel_file
wb.save(decoded_users_all_inf)
Where and what I have put to my code to have this?
Answers:
If you want to sort retrieved_users
, then you can use the built-in list.sort
with a key
to access the sAMAccountName
.
retrieved_users = [
{"attributes": {"sn": "a", "givenName": "Alice", "sAMAccountName": "z"}},
{"attributes": {"sn": "b", "givenName": "Bob", "sAMAccountName": "x"}},
{"attributes": {"sn": "c", "givenName": "Charlie", "sAMAccountName": "y"}},
]
retrieved_users.sort(key=lambda d: d["attributes"]["sAMAccountName"])
retrieved_users
contains
[{'attributes': {'sn': 'b', 'givenName': 'Bob', 'sAMAccountName': 'x'}},
{'attributes': {'sn': 'c', 'givenName': 'Charlie', 'sAMAccountName': 'y'}},
{'attributes': {'sn': 'a', 'givenName': 'Alice', 'sAMAccountName': 'z'}}]
On another note, you can do ws.append(row)
to append entire rows at a time rather than doing ws.cell(row, col, value)
three times:
wb = Workbook()
ws = wb.active
ws.append(('SURNAME', 'NAME', 'sAMAccountName'))
is equivalent to
wb = Workbook()
ws = wb.active
ws.cell(1, 1, 'SURNAME')
ws.cell(1, 2, 'NAME')
ws.cell(1, 3, 'sAMAccountName')
I would like to save sorted data from A to Z by column C in my Excel file.
My code:
### EXCEL
# Set column names
A = 'SURNAME'
B = 'NAME'
C = 'sAMAccountName'
# Set worksheet
wb = Workbook() # create excel worksheet
ws_01 = wb.active # Grab the active worksheet
ws_01.title = "all inf" # Set the title of the worksheet
# Set first row
title_row = 1
ws_01.cell(title_row, 1, A) # cell(row, col, value)
ws_01.cell(title_row, 2, B)
ws_01.cell(title_row, 3, C)
data_row = 2
for user in retrieved_users:
attributes = user['attributes']
sAMAccountName = attributes['sAMAccountName']
if(user_validation(sAMAccountName) == True):
A = attributes['sn']
B = attributes['givenName']
C = sAMAccountName
ws_01.cell(data_row, 1, str(A))
ws_01.cell(data_row, 2, str(B))
ws_01.cell(data_row, 3, str(C))
data_row = data_row + 1
# Save it in an Excel file
decoded_users_all_inf = root_path + reports_dir + users_all_inf_excel_file
wb.save(decoded_users_all_inf)
Where and what I have put to my code to have this?
If you want to sort retrieved_users
, then you can use the built-in list.sort
with a key
to access the sAMAccountName
.
retrieved_users = [
{"attributes": {"sn": "a", "givenName": "Alice", "sAMAccountName": "z"}},
{"attributes": {"sn": "b", "givenName": "Bob", "sAMAccountName": "x"}},
{"attributes": {"sn": "c", "givenName": "Charlie", "sAMAccountName": "y"}},
]
retrieved_users.sort(key=lambda d: d["attributes"]["sAMAccountName"])
retrieved_users
contains
[{'attributes': {'sn': 'b', 'givenName': 'Bob', 'sAMAccountName': 'x'}},
{'attributes': {'sn': 'c', 'givenName': 'Charlie', 'sAMAccountName': 'y'}},
{'attributes': {'sn': 'a', 'givenName': 'Alice', 'sAMAccountName': 'z'}}]
On another note, you can do ws.append(row)
to append entire rows at a time rather than doing ws.cell(row, col, value)
three times:
wb = Workbook()
ws = wb.active
ws.append(('SURNAME', 'NAME', 'sAMAccountName'))
is equivalent to
wb = Workbook()
ws = wb.active
ws.cell(1, 1, 'SURNAME')
ws.cell(1, 2, 'NAME')
ws.cell(1, 3, 'sAMAccountName')