您当前的位置:首页 > 教师团队 > 信息组 > 教研活动

成绩分析系统之读写EXCEL数据(python代码)


时间:2022-09-21    作者:    阅读:


任务1:
  #写入excel数据并保存文件

import xlwt   #导入excel写模块
import sys    #导入系统模块
xl = xlwt.Workbook(encoding='utf-8')  # 创建一个工作簿
# 创建一个sheet对象,第二个参数是指单元格是否允许重设置,默认为False
sheet = xl.add_sheet('我的工作薄', cell_overwrite_ok=False)

sheet.write(0, 0, '数据1')  #写入0行0列
sheet.write(3, 2, '数据2')  #写入3行2列
sheet.write(1, 5, '123456')  #写入1行5列
sheet.write(5, 1, '34545454')  #写入5行1列
xl.save(sys.path[0]+'\\test.xls')  #保存文件    其中sys.path[0]表示当前工作目录

任务2:
 #读写Excel数据

import xlrd     #导入excel读模块
import xlwt   #导入excel写模块
import sys    #导入系统模块
data = xlrd.open_workbook(sys.path[0]+"\\cj.xls")   #打开当前工作目录cj.xls文件
table1 = data.sheets()[0]     #打开第一个表文件
#获取第一行数据  table_list为列表
table_list = table1.row_values(rowx=0, start_colx=0, end_colx=None)  
xl = xlwt.Workbook(encoding='utf-8')  # 创建一个工作簿
# 创建一个sheet对象,第二个参数是指单元格是否允许重设置,默认为False
sheet = xl.add_sheet('我的工作薄', cell_overwrite_ok=False)

sheet.write(0, 0, table_list[0])  #写入0行0列
sheet.write(0, 1, table_list[1])  #写入0行1列
sheet.write(0, 2, table_list[2])  #写入0行2列
sheet.write(0, 3, table_list[3])  #写入0行3列

xl.save(sys.path[0]+'\\test.xls')  #保存文件    其中sys.path[0]表示当前工作目录

excel成绩表样本:下载

 

版权属于:怀铁一中  设计维护:怀铁一中信息中心
学校联系电话:0745-2788218
湘ICP备05012886号   湘教QS7-201306-001563

湘公网安备 43120202000135号