Python之excel文件追加內容


首先要安裝三個模塊: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") # 保存並覆蓋文件

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM