openpyxl is a third-party library that can handle Excel files in xlsx format.
Install:
pip install openpyxl
Read the following excel, as shown in the figure:
from openpyxl import load_workbook
wb = load_workbook("123.xlsx")
print(wb.get_sheet_names())
#>>>[Sheet111,Sheet222,Sheet333]
a_sheet = wb.get_sheet_by_name("Sheet111")
print(a_sheet.title)
#>>>Sheet111
a1 = a_sheet['A1']
print(f'({a1.column}, {a1.row}) is {a1.value}')
#>>>3
a1_too =a_sheet.cell(row = 4, column = 2)
print(a1_too.value)
#>>>fvf
#Get the max column and max row
print (a_sheet.max_row)
# >>>6
print (a_sheet.max_column)
# >>>4
for row in a_sheet.rows:
for cell in row:
print(cell.value,end = '\t')
print('\n')
'''
>>>
3 3 None None
4 None None None
5 None fv fv
6 fvf None None
7 None None None
909 None None None
'''
#Get the data of a row or column
for cell in list(a_sheet.rows)[3 ]:
print (cell.value,end = " \t " )
# >>>6 fvf None None
#Get cells in any interval
for i in range(2, 4 ):
for j in range(1, 3 ):
print (a_sheet.cell(row=i, column=j).value,end = " \t " )
# >>>4 None 5 None
Write operations to files:
Write tomorrow, too sleepy, sleep
Reference article:
http://www.pythontab.delete Chinese characters.com/html/2018/pythonhexinbiancheng_0503/1286.html
(I don't know why the link to the Python Chinese development community can't be put up)