-
前言
xlrd庫用於讀取excel文件中的數據,xlwt庫用於將數據寫入excel文件,修改用xlutils模塊;
xlutils庫也僅僅是通過復制一個副本進行操作后保存一個新文件,像是xlrd庫和xlwt庫之間的一座橋梁,需要依賴於xlrd和xlwt兩個庫 -
xlutils實戰
import os
import xlrd
from xlutils.copy import copy
excel_path = os.path.join(os.path.dirname(__file__),'data/test_data.xls') #修改excel操作,暫時不支持xlsx,修改之前需要將格式改成xls
wb = xlrd.open_workbook(excel_path,formatting_info=True) #加上formatting_info拷貝附件的時候將格式一並拷貝
new_wb = copy(wb) #創建一個副本對象
ws = new_wb.get_sheet(wb.sheet_names().index('Sheet1')) #副本對象沒有sheet_by_name,或者sheet_by_index,通常用get_sheet方法
ws.write(1,3,60) #write(row坐標,col坐標,修改的值)
new_wb.save(excel_path)