python读写Excel数据,xlwt,xlrd,xlutils写入数据到已存在的Excel表格中

一、用到的库:xlwt,xlrd,xlutils

二、通过xlrd读取Excel数据:

import xlrd

# 通过open_workbook读取Excel文件
data_r = xlrd.open_workbook("资产盘点记录表.xls")
#通过索引将sheet表赋值给变量 table_r = data_r.sheets()[0]
#获取整列数据(列索引) col_value = table_r.col_values(1)
#获取整行数据(行索引) row_valu e= table_r.row_values(1)
#获取单个单元格数据(行索引,列索引) value = sheet1.cell(1,2).value

三、通过xlwt创建新表写入数据

import xlwt
# 创建Excel文件对象、表格页sheet workbook = xlwt.Workbook(encoding='utf-8') worksheet = workbook.add_sheet('sheet名称') # sheet页中写入数据 worksheet.write(0, 0, label='ID') worksheet.write(0, 1, label='姓名') # 保存Excel文件 workbook.save(file_name)

四、通过xlutils在已有表中写数据

import xlrd,xlwt
from xlutils.copy import copy

# 将已存在的Excel表格赋值给变量
excel_file = xlrd.open_workbook("准备导入的资产.xls")
# 复制Excel excel_file_copy= copy(data_w2)
# 根据索引获取要写入数据sheet sheet_index = excel_file_copy.get_sheet(0)
# 写入数据 sheet_index.write(1, 2, "张三")
# 保存文件 excel_file_copy.save('准备导入的资产.xls')