Friday, January 6, 2023

Openpyxl part 2,accessing data in excel using python

 import openpyxl
work_book=openpyxl.load_workbook(r'C:\Users\allso\Desktop\new vba projects\openpyxl\countries.xlsx')
print(work_book.sheetnames)
sheet_obj=work_book['Sheet1']
print(sheet_obj.title)
print(sheet_obj['A1'])
cell=sheet_obj['B1']
print(cell.value)
print(cell.row)
print(cell.column)
print(cell.number_format)
print(cell.coordinate)
print(cell.data_type)
print(sheet_obj['A2'].value+\
      '\'s capital  '+sheet_obj['B2'].value+\
      ' has a population of '+str(sheet_obj['C2'].value)\
      )
print(sheet_obj.cell(row=1,column=2))
print(sheet_obj.cell(row=1,column=2).value)
print(sheet_obj.max_row)
print(sheet_obj.max_column)
max_col=sheet_obj.max_column
for i in range(1,max_col+1):
    cell_obj=sheet_obj.cell(row=1,column=i)
    print(cell_obj.value)

max_row=sheet_obj.max_row
for i in range(1,max_row+1):
    cell_obj=sheet_obj.cell(row=i,column=1)
    print(cell_obj.value)

print(sheet_obj['A1':'C2'])
for rows in sheet_obj['A1':'C2']:
    for cell in rows:
        print(cell.coordinate,cell.value)
    print('--------------------------------')   
    


No comments:

Post a Comment