# Python reads Excel from Import = Load_workbook ('test.xlsx')print(wb.sheetnames)
from openpyxl import LOAD_WORKBOOKWB = Load_workbook (" test.xlsx " ) sheet = Wb.get _sheet_by_name ( ' sheet1 " ) print (sheet[< Span style= "COLOR: #800000" > " a " ]) #列表A的所有数据
>>> (<cell ' Sheet1 '). A1>, <cell ' Sheet1 '. A2>, <cell ' Sheet1 '. A3>, <cell ' Sheet1 '. A4>, <cell ' Sheet1 '. A5>, <cell ' Sheet1 '. A6>, <cell ' Sheet1 '. A7>, <cell ' Sheet1 '. A8>, <cell ' Sheet1 '. A9>, <cell ' Sheet1 '. a10>)
Print (sheet[' 4 ']) #列表第四行的所有内容
>>> (<cell ' Sheet1 '. A4>, <cell ' Sheet1 '. B4>, <cell ' Sheet1 '. C4>, <cell ' Sheet1 '. D4>, <cell ' Sheet1 '. e4>)
Print (sheet[' C4 '].value)
>>> Oh wow
For i in sheet[' C ']:
Print (I.value)
>>>c1
C2
C3
Oh, whoa.
C5
C6
C7
C8
C9
C10
#Python writes to Excel fromOpenpyxlImportWORKBOOKWB=Workbook () sheet=Wb.activesheet.title='New Sheet' #定义sheet的标题, the sheet name belowsheet['C3'] ='Hello' #C列, third line, write Hello forIinchRange (10): sheet['a%d'% (i+1)].value = i+1# loops through 0 to 9, then i+1 is 1 to 10, which means a1-a10, write 1-10sheet['E1'].value ='=sum (A:A)' #在E1写入A列所有数据的和Wb.save ('Save a new excel.xlsx') #保存到一个新的excel
Python read-write operation for Excel-----OPENPYXL