Python USES the third party library xlutils to append to the Excel file example

  • 2020-05-05 11:26:12
  • OfStack

There is no better way to write Excel, lorinnn found on the Internet and used after is to use the third party library xlutils to achieve this function, the main idea is to copy a copy of Sheet and then append to it again and save it in a new Excel document.

Using xlutils

The code implementation is 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")

After

before writing

name sex  age country
jim  man  19 USA
hmm  woman 24 CHN
lilei man  24 CHN

Chase after write


name  sex  age country
jim  man  19 USA
hmm  woman 24 CHN
lilei man  24 CHN
Ann  woman 22 UK


Related articles: