我的账户
码尚云教育

云计算企业项目实战营

亲爱的游客,欢迎!

已有账号,请

如尚未注册?

python读取Excel实例

[复制链接]
云鼎 发表于 2022-9-19 14:24:42 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题
1.操作步骤:
(1)安装python官方Excel库-->xlrd
(2)获取Excel文件位置并读取
(3)读取sheet
(4)读取指定rows和cols内容

2. 示例代码
  • # -*- coding: utf-8 -*-
  • import xlrd
  • from datetime import date,datetime
  • def read_excel():
  • #文件位置
  • ExcelFile=xlrd.open_workbook(r'C:\Users\Administrator\Desktop\TestData.xlsx')
  • #获取目标EXCEL文件sheet名
  • print ExcelFile.sheet_names()
  • #------------------------------------
  • #若有多个sheet,则需要指定读取目标sheet例如读取sheet2
  • #sheet2_name=ExcelFile.sheet_names()[1]
  • #------------------------------------
  • #获取sheet内容【1.根据sheet索引2.根据sheet名称】
  • #sheet=ExcelFile.sheet_by_index(1)
  • sheet=ExcelFile.sheet_by_name('TestCase002')
  • #打印sheet的名称,行数,列数
  • print sheet.name,sheet.nrows,sheet.ncols
  • #获取整行或者整列的值
  • rows=sheet.row_values(2)#第三行内容
  • cols=sheet.col_values(1)#第二列内容
  • print cols,rows
  • #获取单元格内容
  • print sheet.cell(1,0).value.encode('utf-8')
  • print sheet.cell_value(1,0).encode('utf-8')
  • print sheet.row(1)[0].value.encode('utf-8')
  • #打印单元格内容格式
  • print sheet.cell(1,0).ctype


[color=rgb(15, 199, 122) !important]复制代码

[color=rgb(15, 199, 122) !important]


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注我们
码尚云云计算培训

客服电话:18710061913

周一至周日: 7:00-24:00

码尚云教育( 陕ICP备2023009934号-2 )

Powered by Discuz! X3.4© 2016-2023 Comsenz Inc.