#给excel文件指定单元格中写入数据
"""
实现思路:
1.打开需要写入数据的excel文件
2.使用copy方法xlrd对象的文件转换成xlwt对象的文件
3.指定excel文件的sheet页签
4.指定单元格写入数据
5.保存文件
"""
1 import xlrd
2 from xlutils.copy import copy
3
4 def write_excel(fileName, sheetName, col, row, text):
5 '''
6 @param fileName: excel文件路径及名称
7 @param sheetName: excel文件的sheet页签名称
8 @param col: 行坐标
9 @param row: 列坐标
10 @param text: 内容
11 @return:
12 '''
13 excel = xlrd.open_workbook(fileName, formatting_info=True) # formatting_info=True保留原有的数据格式
14 new_excel = copy(excel) # 将xlrd对象拷贝转换成xlwt对象
15 sheet = new_excel.get_sheet(sheetName) # 通过sheet名称获取表格
16 sheet.write(col, row, text) # 指定单元格中写入数据
17 new_excel.save(filename) # 保存excel文件