1. 程式人生 > >Python之excel文件追加內容

Python之excel文件追加內容

test 內容 body util div utf-8 imp import odi

首先要安裝三個模塊:xlrd,xlwt,xlutils

命令:pip install xlrd xlwt xlutils

示例代碼:

 1 #!/usr/bin/env python
 2 # -*- coding:utf-8 -*-
 3 
 4 from xlrd import open_workbook
 5 from xlutils.copy import copy
 6 
 7 r_xls = open_workbook("test.xls") # 讀取excel文件
 8 row = r_xls.sheets()[0].nrows # 獲取已有的行數
 9 excel = copy(r_xls) #
將xlrd的對象轉化為xlwt的對象 10 table = excel.get_sheet(0) # 獲取要操作的sheet 11 12 #對excel表追加一行內容 13 table.write(row, 0, 內容1) #括號內分別為行數、列數、內容 14 table.write(row, 1, 內容2) 15 table.write(row, 2, 內容3) 16 17 excel.save("test.xls") # 保存並覆蓋文件

Python之excel文件追加內容