Python Excel数据处理之xlrd/xlwt/xlutils模块详解

 更新时间:2023年03月21日 09:02:04   作者:Sir 老王  
在复杂的Excel业务数据处理中,三兄弟扮演的角色缺一不可。如何能够使用xlrd/xlwt/xlutils三个模块来实现数据处理就是今天的内容,希望对大家有所帮助

常规的Excel数据处理中,就是对Excel数据文件的读/写/文件对象操作。

通过对应的python非标准库xlrd/xlwt/xlutils,来实现具体的数据处理业务逻辑。

在复杂的Excel业务数据处理中,三兄弟扮演的角色缺一不可。如何能够使用xlrd/xlwt/xlutils三个模块来实现数据处理就是今天的内容。

1、模块说明

使用该三个模块来处理Excel数据最好的地方就是他们和Excel文件对象对应的数据处理概念是一样的,能更好的便于我们理解数据对象。

首先,这三个模块都是python的非标准库,可以选择pip的方式来进行安装。

pip install xlrd

pip install xlwt

pip install xlutils

下面是我们为演示数据处理的过程准备的源数据内容,只是用于测试。

xlrd:用于读取Excle数据文件将返回的数据对象放到内存中,然后查询数据文件对象的相关信息。

xlwt:用于在内存中生成新的数据文件对象,处理完成后写入到Excel数据文件中。

xlutils:主要的作用就是copy新的文件对象,在新的数据对象中完成数据处理操作。

将xlrd/xlwt/xlutils三个模块分别都导入到待开发的代码块中提供支持。

# Importing the xlrd module.
import xlrd as read

# Importing the xlwt module.
import xlwt as write

# Copying the contents of the original workbook into a new workbook.
from xlutils.copy import copy

2、xlrd处理

# Opening the workbook and assigning it to the variable `work_book`.
work_book = read.open_workbook('D:/test-data-work/test.xls')

# Assigning the sheet named 'Sheet1' to the variable `sheet`.
sheet = work_book.sheet_by_name('Sheet1')

# `row = sheet.nrows` is assigning the number of rows in the sheet to the variable `row`.
row = sheet.nrows

# `col = sheet.ncols` is assigning the number of columns in the sheet to the variable `col`.
col = sheet.ncols

print('Sheet1工作表有:{0}行,{1}列'.format(str(row), str(col)))

# Sheet1工作表有:23行,5列

下面是三种常用的sheet对象的数据遍历方式,分别是按行/列的方式进行数据遍历。

for a in sheet.get_rows():
    print(a)

# [text:'姓名', text:'年龄', text:'班级', text:'成绩', text:'表现']
# [text:'Python 集中营', number:20.0, number:1210.0, number:90.0, text:'A']
# [text:'Python 集中营', number:21.0, number:1211.0, number:91.0, text:'A']
# [text:'Python 集中营', number:22.0, number:1212.0, number:92.0, text:'A']
# [text:'Python 集中营', number:23.0, number:1213.0, number:93.0, text:'A']
# [text:'Python 集中营', number:24.0, number:1214.0, number:94.0, text:'A']
# [text:'Python 集中营', number:25.0, number:1215.0, number:95.0, text:'A']
# [text:'Python 集中营', number:26.0, number:1216.0, number:96.0, text:'A']
# [text:'Python 集中营', number:27.0, number:1217.0, number:97.0, text:'A']
# [text:'Python 集中营', number:28.0, number:1218.0, number:98.0, text:'A']
# [text:'Python 集中营', number:29.0, number:1219.0, number:99.0, text:'A']
# [text:'Python 集中营', number:30.0, number:1220.0, number:100.0, text:'A']
# [text:'Python 集中营', number:31.0, number:1221.0, number:101.0, text:'A']
# [text:'Python 集中营', number:32.0, number:1222.0, number:102.0, text:'A']
# [text:'Python 集中营', number:33.0, number:1223.0, number:103.0, text:'A']
# [text:'Python 集中营', number:34.0, number:1224.0, number:104.0, text:'A']
# [text:'Python 集中营', number:35.0, number:1225.0, number:105.0, text:'A']
# [text:'Python 集中营', number:36.0, number:1226.0, number:106.0, text:'A']
# [text:'Python 集中营', number:37.0, number:1227.0, number:107.0, text:'A']
# [text:'Python 集中营', number:38.0, number:1228.0, number:108.0, text:'A']
# [text:'Python 集中营', number:39.0, number:1229.0, number:109.0, text:'A']
# [text:'Python 集中营', number:40.0, number:1230.0, number:110.0, text:'A']
# [text:'Python 集中营', number:41.0, number:1231.0, number:111.0, text:'A']

for b in range(row):
    print(sheet.row_values(b))

# ['姓名', '年龄', '班级', '成绩', '表现']
# ['Python 集中营', 20.0, 1210.0, 90.0, 'A']
# ['Python 集中营', 21.0, 1211.0, 91.0, 'A']
# ['Python 集中营', 22.0, 1212.0, 92.0, 'A']
# ['Python 集中营', 23.0, 1213.0, 93.0, 'A']
# ['Python 集中营', 24.0, 1214.0, 94.0, 'A']
# ['Python 集中营', 25.0, 1215.0, 95.0, 'A']
# ['Python 集中营', 26.0, 1216.0, 96.0, 'A']
# ['Python 集中营', 27.0, 1217.0, 97.0, 'A']
# ['Python 集中营', 28.0, 1218.0, 98.0, 'A']
# ['Python 集中营', 29.0, 1219.0, 99.0, 'A']
# ['Python 集中营', 30.0, 1220.0, 100.0, 'A']
# ['Python 集中营', 31.0, 1221.0, 101.0, 'A']
# ['Python 集中营', 32.0, 1222.0, 102.0, 'A']
# ['Python 集中营', 33.0, 1223.0, 103.0, 'A']
# ['Python 集中营', 34.0, 1224.0, 104.0, 'A']
# ['Python 集中营', 35.0, 1225.0, 105.0, 'A']
# ['Python 集中营', 36.0, 1226.0, 106.0, 'A']
# ['Python 集中营', 37.0, 1227.0, 107.0, 'A']
# ['Python 集中营', 38.0, 1228.0, 108.0, 'A']
# ['Python 集中营', 39.0, 1229.0, 109.0, 'A']
# ['Python 集中营', 40.0, 1230.0, 110.0, 'A']
# ['Python 集中营', 41.0, 1231.0, 111.0, 'A']

for c in range(col):
    print(sheet.col_values(c))

# ['姓名', 'Python 集中营', 'Python 集中营', 'Python 集中营', 'Python 集中营', 'Python 集中营', 'Python 集中营', 'Python 集中营', 'Python 集中营', 'Python 集中营', 'Python 集中营', 'Python 集中营', 'Python 集中营', 'Python 集中营', 'Python 集中营', 'Python 集中营', 'Python 集中营', 'Python 集中营', 'Python 集中营', 'Python 集中营', 'Python 集中营', 'Python 集中营', 'Python 集中营']
# ['年龄', 20.0, 21.0, 22.0, 23.0, 24.0, 25.0, 26.0, 27.0, 28.0, 29.0, 30.0, 31.0, 32.0, 33.0, 34.0, 35.0, 36.0, 37.0, 38.0, 39.0, 40.0, 41.0]
# ['班级', 1210.0, 1211.0, 1212.0, 1213.0, 1214.0, 1215.0, 1216.0, 1217.0, 1218.0, 1219.0, 1220.0, 1221.0, 1222.0, 1223.0, 1224.0, 1225.0, 1226.0, 1227.0, 1228.0, 1229.0, 1230.0, 1231.0]
# ['成绩', 90.0, 91.0, 92.0, 93.0, 94.0, 95.0, 96.0, 97.0, 98.0, 99.0, 100.0, 101.0, 102.0, 103.0, 104.0, 105.0, 106.0, 107.0, 108.0, 109.0, 110.0, 111.0]
# ['表现', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A']

3、xlwt处理

# Creating a new workbook.
work_book_2 = write.Workbook()

# Creating a new sheet named 'Sheet4' in the workbook.
sheet_2 = work_book_2.add_sheet('Sheet4')

list = [
    ['姓名', '年龄', '班级', '成绩'],
    ['张三', '20', '1210', '89'],
    ['李四', '21', '1211', '90'],
    ['王五', '22', '1212', '91'],
]
for row_index in range(4):
    for col_index in range(4):
        sheet_2.write(row_index, col_index, list[row_index][col_index])
        col_index += 1
    row_index += 1

# Saving the workbook to the specified location.
work_book_2.save('D:/test-data-work/test2.xls')

4、xlutils处理

# Opening the workbook and assigning it to the variable `work_book_3`.
work_book_3 = read.open_workbook('D:/test-data-work/test.xls')

# Copying the contents of the original workbook into a new workbook.
work_book_3_copy = copy(work_book_3)

# Saving the contents of the original workbook into a new workbook.
work_book_3_copy.save('D:/test-data-work/test3.xls')

到此这篇关于Python Excel数据处理之xlrd/xlwt/xlutils模块详解的文章就介绍到这了,更多相关Python Excel数据处理内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Python求两点之间的直线距离(2种实现方法)

    Python求两点之间的直线距离(2种实现方法)

    今天小编就为大家分享一篇Python求两点之间的直线距离(2种实现方法),具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-07-07
  • 使用Django+Pytest搭建在线自动化测试平台

    使用Django+Pytest搭建在线自动化测试平台

    最近由于公司的发展安排本人实现公司项目的自动化测试,下面这篇文章主要给大家介绍了关于如何Django + Pytest搭建在线自动化测试平台的相关资料,文中通过实例代码介绍的非常详细,需要的朋友可以参考下
    2022-07-07
  • Python开发的实用计算器完整实例

    Python开发的实用计算器完整实例

    这篇文章主要介绍了Python开发的实用计算器,结合完整实例形式分析了Python实现计算器四则运算、开方、取余等相关操作技巧,需要的朋友可以参考下
    2017-05-05
  • Python VTK计算曲面的高斯曲率和平均曲率

    Python VTK计算曲面的高斯曲率和平均曲率

    这篇文章主要介绍了Python VTK计算曲面的高斯曲率和平均曲率,如何使用户Python版本的VTK计算曲面的高斯曲率并映射在曲面上。本例中使用了两个不同的表面,每个表面根据其高斯曲率和平均曲率着色,需要的朋友可以参考一下
    2022-04-04
  • Python两个字典键同值相加的几种方法

    Python两个字典键同值相加的几种方法

    今天小编就为大家分享一篇关于Python两个字典键同值相加的几种方法,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2019-03-03
  • python中opencv与PIL图片读取保存及相互转化方式

    python中opencv与PIL图片读取保存及相互转化方式

    这篇文章主要介绍了python中opencv与PIL图片读取保存及相互转化方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-09-09
  • Python实现双人五子棋对局

    Python实现双人五子棋对局

    这篇文章主要为大家详细介绍了Python实现双人五子棋对局,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-05-05
  • python Pydub简单易用的音频处理库使用实例探索

    python Pydub简单易用的音频处理库使用实例探索

    Pydub是一个简单易用的Python库,它让音频处理变得像处理列表或字符串一样简单,你可以用Pydub来剪辑、合并、调整音频文件,以及执行许多其他的音频处理任务,它支持多种音频格式,包括常见的MP3、WAV和AAC
    2024-01-01
  • python 实现上传图片并预览的3种方法(推荐)

    python 实现上传图片并预览的3种方法(推荐)

    下面小编就为大家带来一篇python 实现上传图片并预览的3种方法(推荐)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-07-07
  • Python实现Appium端口检测与释放的实现

    Python实现Appium端口检测与释放的实现

    这篇文章主要介绍了Python实现Appium端口检测与释放的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-12-12

最新评论