Python表格读写的简单实例

原文地址

  • 读取表格内容

需要xlrd这个模块包的支持

import xlrd

# 获取表格对象
xlsx = xlrd.open_workbook('data/e03.xlsx')  # 打开表格

# 获取sheet对象的两种方式
table = xlsx.sheet_by_index(0)  # 通过sheet索引获取
# table = xlsx.sheet_by_name('全部付款方式')  # 通过sheet名称获取

# 获得单元格值的三种方式
print(table.cell_value(5, 3))
print(table.cell(1, 2).value)
print(table.row(1)[2].value)
  • 表格写入

需要xlwt这个模块支持

import xlwt

# 获取表格对象
new_workboot = xlwt.Workbook()

# 获取sheet对象
worksheet = new_workboot.add_sheet('new_sheet')	# (sheet名称)

# 写入单元格内容
worksheet.write(0, 0, 'test00')	# (行,列,写入内容)

# 表格保存
new_workboot.save('./data/newxl.xls')

参考文献

学习地址

发布了102 篇原创文章 · 获赞 68 · 访问量 5124

猜你喜欢

转载自blog.csdn.net/BBJG_001/article/details/104206267
今日推荐