背景
Python中,xlrd主要用来读取excel文件, xlwt主要用来写文件,本文主要介绍打开已经存在的excel的xls文件,然后在最后新的一行的数据。要用到xlutils包,它依赖于前两个包。
折腾过程
1.找到了参考资料:
writing to existing workbook using xlwt
其实是没有直接实现:
打开已有的excel文件,然后在文件最后写入,添加新数据
的函数的。
只不过,可以利用:
Working with Excel Files in Python
中的库,组合实现。
2. writing to existing workbook using xlwt
给出了示例代码:
?
12345678910111213141516171819202122
rom xlutils.copy importcopy fromxlrd importopen_workbook fromxlwt importeasyxf START_ROW =297 # 0 based (subtract 1 from excel row number)col_age_november =1col_summer1 =2col_fall1 =3 rb =open_workbook(file_path,formatting_info=True)r_sheet =rb.sheet_by_index(0) # read only copy to introspect the filewb =copy(rb) # a writable copy (I can't read values out of this, only write to it)w_sheet =wb.get_sheet(0) # the sheet to write to within the writable copy forrow_index inrange(START_ROW, r_sheet.nrows): age_nov =r_sheet.cell(row_index, col_age_november).value ifage_nov ==3: #If 3, then Combo I 3-4 year old for both summer1 and fall1 w_sheet.write(row_index, col_summer1, 'Combo I 3-4 year old') w_she