參考資料:
https://www.testwo.com/blog/7269
#導入openpyxl from openpyxl import load_workbook #創建文件路徑 path=r"E:\" filename=“test.xlsx” filepath=path+filename #打開excel xl=load_workbook(filepath) #獲取所有sheet頁名字 xl_sheet_names=xl.get_sheet_names() #打印所有sheet頁名稱 print(xl_sheet_names) #定位到相應sheet頁,[0]為sheet頁索引 xl_sheet=xl.get_sheet_by_name(xl_sheet_names[0]) #獲取行列數 row=xl_sheet.max_row column=xl_sheet.max_column #獲取單元格值 xl_cell=xl_sheet.cell(row=i,column=j).value #單元格賦值 xl_cell=xl_sheet.cell(row=i,column=j,value=“金額”) #保存excel xl.save(filepath)