Home > database >  how can I make a python dictionary from excel file using openpyxl?
how can I make a python dictionary from excel file using openpyxl?

Time:12-01

simply I have 2 columns in excel file . lets say A and B after reading the excel file I want to store the values in dictionary like this : {A1:B1, A2:B2,.....} until the end of columns contents. How can I do that. thanks in advance.

CodePudding user response:

I wrote demo code as below, hope is helps.

import openpyxl
workbook = openpyxl.load_workbook('./test.xlsx')
worksheet = workbook.active

dictionary = {}

for row in range(1, worksheet.max_row   1):
  key = worksheet.cell(row, 1).value
  value = worksheet.cell(row, 2).value
  dictionary[key] = value

print(dictionary)

  • Related