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

Python之Excel文件处理

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

Python之Excel文件处理

文章目录

一、xlrd和xlwt模块二、获取sheet三、关于cell的相关操作四、cell常用数据类型五、将数据写入Excel文件六、编辑Excel文件

一、xlrd和xlwt模块

1.python操作Excel表格需要导入两个模块:xlrd和xlwt
两个模块都非系统自带,需手动安装,安装方式这里提供两种:
(1)去python官网安装
(2)可到编译器终端进行安装,安装命令为:pip install 模块名

2.xlrd用于读取Excel文件,xlwt用于将数据写入、编辑Excel文件;
现版本的xlrd和xiwt都只支持读取、写入后缀为.xls的文件,不可用于后缀为.xlsx的文件

3.用xlrd模块读取文件时如果出现报错“xlrd.biffh.XLRDError: Excel xlsx file; not supported”,原因在于现版本的xlrd模块不支持读取后缀为.xlsx的文件,只支持读取后缀为.xls的文件
若想用读取后缀为.xlsx的文件,则需要删除现版本xlrd模块,安装1.2.0版本的xlrd模块,具体操作步骤如下:
(1)卸载命令为:pip uninstall xlrd
(2)安装命令为:pip install xlrd==1.2.0


二、获取sheet
import xlrd

# sheet_names:获取所有的sheet的名字
workbook = xlrd.open_workbook("运动记录.xlsx")
print(workbook.sheet_names())

# sheet_by_name:根据名称来获取指定的sheet对象
sheet = workbook.sheet_by_name("第一周")
print(sheet.name)

# sheet_by_index:根据索引来获取指定的sheet对象
sheet = workbook.sheet_by_index(1)
print(sheet.name)

# 获取所有的sheet对象
sheets = workbook.sheets()
for sheet_ in sheets:
    print(sheet_.name, end=" ")
print()

# sheet.nrows:获取指定sheet中的行数
# sheet.ncols:获取指定sheet中的列数
sheet1 = workbook.sheet_by_index(0)
print(sheet1.nrows, sheet1.ncols)

三、关于cell的相关操作
import xlrd

# sheet.cell(row,col):获取指定行和列的cell对象,返回值的类型为xlrd.sheet.Cell
workbook = xlrd.open_workbook("运动记录.xlsx")
sheet1 = workbook.sheet_by_index(0)
cell = sheet1.cell(1, 0)
print(cell.value)

# sheet.cell_value(row,col):获取指定行和列的值,返回的是字符串
cell_ = sheet1.cell_value(1, 0)
print(cell_)


# sheet.row_slice(row,start_col,end_col):获取指定行的某几列对象,返回一个列表,里面各数据的类型为xlrd.sheet.Cell
cells = sheet1.row_slice(1, 1, 9)
for i in cells:
    print(i.value, end=' ')

# sheet.row_values(row,start_col,end_col):获取指定行的某几列的值,返回一个列表,里面各数据是字符串类型
cells = sheet1.row_values(1, 1, 9)
for i in cells:
    print(i, end=' ')


# sheet.col_slice(col,start_row,end_row):获取指定行的某几列对象,返回一个列表,里面各数据的类型为xlrd.sheet.Cell
cells = sheet1.col_slice(0, 1, sheet1.nrows)
for i in cells:
    print(i.value, end=' ')

# sheet.col_values(col,start_row,end_row):获取指定行的某几列对象,返回一个列表,里面各数据是字符串类型
cells = sheet1.col_values(0, 1, sheet1.nrows)
for i in cells:
    print(i, end=' ')

四、cell常用数据类型
    xlrd.XL_CELL_TEXT(Text):文本类型xlrd.XL_CELL_NUMBER(Number):数值类型xlrd.XL_CELL_DATE(Data):日期时间类型xlrd.XL_CELL_BOOLEAN(Bool):布尔类型xlrd.XL_CELL_EMPTY:空白数据类型

五、将数据写入Excel文件

1.创建一个Workbook对象
2.创建一个sheet对象
3.使用sheet.write方法把数据写入到heet中的指定行列,如果想要在原来Workbook对象上添加新的cell,则需要调用put_cell来添加
4.保存Excel文件

import xlwt
import random

workbook = xlwt.Workbook()
sheet = workbook.add_sheet("第一周运动记录")
headers = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
for index, header in enumerate(headers):
    sheet.write(0, index+1, header)
names = ['chen', 'lu', 'zhang', 'cai']
for index, name in enumerate(names):
    sheet.write(index+1, 0, name)

for row in range(1, 5):
    for col in range(1, 8):
        sheet.write(row, col, random.randint(5, 20))
workbook.save("运动记录1.0.xls")

六、编辑Excel文件

1.读取某个Excel文件
2.在读取的sheet上面对cell进行修改
3.重新创建一个新的Excel文件,把之前读取的数据写入到新的Excel文件中

import xlrd
import xlwt

workbook = xlrd.open_workbook("运动记录1.0.xls")
sheet = workbook.sheet_by_index(0)
# 求总公里数
sheet.put_cell(0, 8, xlrd.XL_CELL_TEXT, '总公里数', None)
for row in range(1, sheet.nrows):
    cells = sheet.row_values(row, 1, 7)
    total = sum(cells)
    sheet.put_cell(row, 8, xlrd.XL_CELL_NUMBER, total, None)
# 求平均公里数
sheet.put_cell(5, 0, xlrd.XL_CELL_TEXT, '平均公里数', None)
for col in range(1, sheet.ncols):
    cells = sheet.col_values(col, 1, 4)
    avg = sum(cells)/len(cells)
    avg = float('%.2f' % avg)  # 保留两位小数
    sheet.put_cell(5, col, xlrd.XL_CELL_NUMBER, avg, None)
# 写入Excel文件
work_book = xlwt.Workbook()
sheet1 = work_book.add_sheet("sheet123")
for row in range(0, sheet.nrows):
    for col in range(0, sheet.ncols):
        sheet1.write(row, col, sheet.cell_value(row, col))
work_book.save("运动记录2.0.xls")
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/756830.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

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

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