1、读取数据(xlrd)
import xlrd
data_file = '../data/数据/基础数据.xlsx'
'''
1. 打开文件
'''
wb = xlrd.open_workbook(data_file)
sheet_names = wb.sheet_names()
sheet = wb.sheet_by_name("Sheet1")
print(sheet.name)
num_cols = sheet.ncols
print("num_cols:", num_cols)
num_rows = sheet.nrows
print("num_rows:", num_rows)
QuDuanBaseInfor = {}
for i in range(1, num_rows):
chezhan = sheet.cell_value(i,0)
QuDuan = sheet.cell_value(i,1)
CC_num = sheet.cell_value(i,4)
QuDuanBaseInfor[chezhan+QuDuan] = int(CC_num)
print(QuDuanBaseInfor)
2、写入数据(xlwt)
import xlwt
book = xlwt.Workbook(encoding='utf-8',style_compression=0)
sheet = book.add_sheet('原始数据', cell_overwrite_ok=True)
'''
注意,此处的单元格定位是以0开始,不是从1开始
'''
col = ("序号","数据")
for i in range(0,2):
sheet.write(0,i,col[i])
savepath = '../保存文件/result.xls'
book.save(savepath)
3. 在已存在的表中进行修改操作
import xlwt
from xlutils.copy import copy
import xlrd
rb = xlrd.open_workbook('./test.xlsx')
wb = copy(rb)
ws = wb.get_sheet(1)
for i in range(11, 92, 4):
for j in range(4, 24):
ws.write(i-1, j-1, "Hello")
wb.save('./test.xlsx')