操作excel的库有多个,但是xlwt、wlrd只能读写xls文件,而不能操作xlsx文件,所以用openpyxl
读:
from openpyxl import Workbook from openpyxl.reader.excel import load_workbook def read_excel(filepath): wb = load_workbook(filepath) sheets = wb.get_sheet_names() sheet_first = sheets[0] ws = wb.get_sheet_by_name(sheet_first) table_rows = ws.rows table_columns = ws.columns for row in table_rows: for col in row: print(col.value,end=' ') print('')
写:
def write_excel(filepath): wb = Workbook() ws = wb.active ws.title = '标题' rowdata = ['1','2','3','4','5'] ws.append(rowdata) wb.save(filename=filepath)