excel 文件的读取和写入

1、创建excel 文件 test.xls 和写入内容

 2、excel 文件的内容读取

import xlrd

work = xlrd.open_workbook('test.xls','r')
sheet = work.sheet_by_index(0)    

print(sheet.nrows)    # 查看行数
print(sheet.cell_value(1,2))    # 获取单元格内容

3、向 excel 写入内容

import xlrd
from xlutils.copy import copy

'''
1.读取文件,找到对象
2.对之前的文件进行保存
3.找到sheet文件,修改,保存
'''
work = xlrd.open_workbook(‘test.xls’)

old_content = copy(work)

ws = old_content.get_sheet(0)    
ws.write(1,3,'hello')    # 向特定的表格写入内容
old_content.save('test.xls')    # 保存可以为原文件,或者新文件
# old_content.save('new_test.xls')
原文地址:https://www.cnblogs.com/siyu0123/p/12833419.html