栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python 对excel操作用法详解_python在excel上的运用?

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python 对excel操作用法详解_python在excel上的运用?

excel读取文件

文章目录

excel读取文件基本操作有关于cell 表格中的一个格子cell数据类型excel的写入excel 文件的编辑excel文件的写入excel 文件写入

基本操作
属性描述
sheet_names获取表的名字
sheet_by_index使用索引获取指定的对象
sheet_by_name使用名字 获取sheetd对象
shests获取所有的sheet对象
sheet.nrows这个sheet中的行数
sheet.ncols这个sheet中的列数

例如:

import xlrd_compdoc_commented
workbook = xlrd_compdoc_commented.open_workbook("成绩表.xlsx")
#获取所有的sheet名字
print(workbook.sheet_names())#结果:['1班', '2班']
# #根据索引获取指定的sheet对象
sheet = workbook.sheet_by_index(1)
print(sheet.name)#结果:2班
# #根据名称获取指定的sheet对象
sheet = workbook.sheet_by_name("2班")
print(sheet.name)#结果:2班

# #获取所有的sheet对象
sheets = workbook.sheets()
for sheet in sheets:
  print(sheet.name)#结果: 1班 2班
有关于cell 表格中的一个格子
属性描述
cell(row,col)获取指定的行和列
row_slice(row,start_col,end_col)获取指定行的某几列
col_slice(col,start_row,end_col)获取指定列的某几行
cell_value(row,col)获取指定行和列的值
row_values(row,start_col,end_col)获取指定行的某几列
col_values(col,start_row,end_row)获取指定列的某几行

代码

import xlrd_compdoc_commented

workbook = xlrd_compdoc_commented.open_workbook("成绩表.xlsx")
from xlrd.sheet import Cell
sheet = workbook.sheet_by_index(0)
cell = sheet.cell(1,1)
#print(type(cell))
#获取 第一行的1-4列
cells = sheet.row_slice(1,0,4)
for cell in cells:
  print(cell.value)
# 获得第一列的所有行
cells = sheet.col_slice(0,1,sheet.nrows)
for cell in cells:
    print(cell.value)
cell数据类型
属性类型
XL_CELL_TEXT文本类型
XL_CELL_NUMBER数值类型
XL_CELL_DATE日期时间类型
XL_CELL_BOOLEAN布尔类型
XL_CELL_EMPTY空白数据类型
excel的写入
import xlwt
import random
workbook=xlwt.Workbook()
sheet=workbook.add_sheet("sheet1")
headers=['姓名','年龄','成绩']
for index,header in enumerate(headers):
    sheet.write(0,index,header)
names=['张三','李四','王五']
for index,name in enumerate(names):
    sheet.write(index+1,0,name)

for row in range(1,4):
    for col in range(1,4):
        sheet.write(row,col,random.randint(70,100))
workbook.save("xinxi.xls")

excel 文件的编辑
import xlrd_compdoc_commented
import xlwt

#求所有学生成绩总分
rwd=xlrd_compdoc_commented.open_workbook("成绩表.xlsx")#只读
rsheet=rwd.sheet_by_index(0)#代表访问第一个表

rsheet.put_cell(0,4,xlrd_compdoc_commented.XL_CELL_TEXT,"总分",None)
for row in range(1,rsheet.nrows):
    grades=rsheet.row_values(row,1,4)
    #print(grades)
    total=sum(grades)
    rsheet.put_cell(row,4,xlrd_compdoc_commented.XL_CELL_TEXT,total,None)
#求所有同学成绩的平均分
rsheet.put_cell(0,5,xlrd_compdoc_commented.XL_CELL_TEXT,"平均分",None)
for row in range(1,rsheet.nrows):
    grades=rsheet.row_values(row,1,4)
    avge=sum(grades)/3
    print(avge)
    rsheet.put_cell(row,5,xlrd_compdoc_commented.XL_CELL_TEXT,avge,None)

##重写进去

wwb=xlwt.Workbook()
wsheet=wwb.add_sheet("sheet1")
nrows=rsheet.nrows#获得行数
ncols=rsheet.ncols#获得列数
#循环写入
for row in range(0,nrows):
    for col in range(0,ncols):
        wsheet.write(row,col,rsheet.cell_value(row,col))

wwb.save("abc.xls")
excel文件的写入 excel 文件写入
wwb=xlwt.Workbook()
wsheet=wwb.add_sheet("sheet1")
nrows=rsheet.nrows#获得行数
ncols=rsheet.ncols#获得列数
#循环写入
for row in range(0,nrows):
    for col in range(0,ncols):
        wsheet.write(row,col,rsheet.cell_value(row,col))

wwb.save("abc.xls")



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/786998.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号