This article mainly introduces how to use a third-party library xlutils in Python to append and write an Excel file. This article provides an example of append and write effect, if you want to write an Excel file, you can see that there is no better way to search for it. lorinnn found it online and then used the third-party library xlutils to implement this function, the main idea is to copy a Sheet and then append it to a new Excel file.
Use xlutils
The code is implemented as follows:
# -*- coding: utf-8 -*- ''' Created on 2012-12-17 @author: walfred @module: XLRDPkg.write_append @description: ''' import os from xlutils.copy import copy import xlrd as ExcelRead def write_append(file_name): values = ["Ann", "woman", 22, "UK"] r_xls = ExcelRead.open_workbook(file_name) r_sheet = r_xls.sheet_by_index(0) rows = r_sheet.nrows w_xls = copy(r_xls) sheet_write = w_xls.get_sheet(0) for i in range(0, len(values)): sheet_write.write(rows, i, values[i]) w_xls.save(file_name + '.out' + os.path.splitext(file_name)[-1]); if __name__ == "__main__": write_append("./test_append.xls")
Pre-write
name sex age countryjim man 19 USAhmm woman 24 CHNlilei man 24 CHN
Post-write
name sex age countryjim man 19 USAhmm woman 24 CHNlilei man 24 CHNAnn woman 22 UK