【Python开发】Excel的操作之——读取

主要思路参考这篇博客的内容,把地址贴上:

http://www.cnblogs.com/zhoujie/p/python18.html

下面贴上我自己的代码

读取excel数据的demo代码如下:

 1 def read_excel_demo():
 2     # 打开文件
 3     workbook = xlrd.open_workbook(r‘C:\Users\wxz\Desktop\2018年信用卡花费(Kevin).xlsx‘)
 4     # 获取所有sheet
 5     print(‘打印所有sheet表名称:‘,workbook.sheet_names())
 6     sheet1_name = workbook.sheet_names()[0]
 7     sheet2_name = workbook.sheet_names()[1]
 8     # print(sheet1_name)
 9
10     # 根据sheet索引或者名称获取sheet内容
11     sheet1 = workbook.sheet_by_index(0)
12     # TODO 问题
13     # sheet2 = workbook.sheet_by_name(‘sheet1_name‘)
14     # print(sheet2)
15
16     # sheet的名称,行数,列数
17     print(sheet1.name, sheet1.nrows, sheet1.ncols)
18
19     # 获取整行和整列的值(数组)
20     rows = sheet1.row_values(0)
21     cols = sheet1.col_values(0)
22     print(rows)
23     print(cols)
24
25     # 获取单元格内容
26     print(sheet1.cell(0, 0).value)
27     print(sheet1.cell_value(0, 0))
28     print(sheet1.row(0)[0].value)
29
30     # 获取单元格内容的数据类型 ctype :  0 empty,1 string, 2 number, 3 date, 4 boolean, 5 error
31     print(sheet1.cell(0, 0).ctype)
32
33     # 获取日期
34     if sheet1.cell(1, 3).ctype == 3:
35         datetime = xlrd.xldate_as_datetime(sheet1.cell(1, 3).value, workbook.datemode)
36         print(datetime.date())

自己写的实际调用函数如下:

 1 def read_excel():
 2     workbook = xlrd.open_workbook(r‘C:\Users\wxz\Desktop\2018年信用卡花费(Kevin).xlsx‘)
 3     sheet_name = workbook.sheet_names() # 获取所有sheet表名称
 4     sheet_length = sheet_name.__len__() # 获取sheet表长度
 5     print(‘已读取Excel所有sheet表名称:‘)
 6
 7     sheet_num = 0
 8     for name in sheet_name:
 9         print(str([sheet_num]) + ‘:‘ + name)
10         sheet_num = sheet_num + 1
11
12     while True:
13         sheet_index = int(input(‘\n请输入需要打印的sheet表序号:‘))
14         if sheet_index < sheet_length:
15             print(‘\n正在打印《‘ + workbook.sheet_names()[sheet_index] + ‘》的内容……\n‘)
16
17             current_sheet = workbook.sheet_by_index(sheet_index)
18             nrows = current_sheet.nrows
19             for n in range(nrows):
20                 print(current_sheet.row_values(n))
21
22             print(‘\n共有‘ + str(nrows) + ‘行,已全部打印完毕\n‘)
23             break;
24         else:
25             print(‘错误提示:sheet表序号超出最大长度,请重新输入‘)
26
27 if __name__ == ‘__main__‘:
28     read_excel()

另外我采用的是bat批处理文件调用py文件,bat代码如下:

1 @echo off
2
3 echo 开始运行ExcelRead程序
4 echo5
6 python G:\PycharmProjects\【180817】ExcelOperation\ExcelRead.py
7
8 pause

执行效果如下:

下一步计划需要把读出来的数据按表排列………(未完待续)

原文地址:https://www.cnblogs.com/Kevin-WangXinzheng/p/9495975.html

时间: 2024-08-30 05:20:49

【Python开发】Excel的操作之——读取的相关文章

python之excel读写操作

一.xlrd和xlwt安装 1.下载xlwt安装包https://pypi.org/project/xlwt/#files 2.解压后进入文件目录 3.执行python setup.py install 二.读取操作 1 # -*- conding:utf-8 -*- 2 __author__ = 'dsh' 3 # How to read from an Excel using xlrd module 4 import xlrd 5 # 关联指定路径中的xls文件,得到book对象 6 fil

python中Excel表操作

python中关于excel表个的操作 使用 python中的xlwt和xlrd模块进行操作 # 2003之前:Excel:xls# 2003之后:Excel:xlsx# xlrd:读取的模块:xls,xlsx# xlwt:写分模块:xls# openpyxl:既能读也能写,只能操作xlsx 所以后面一般操作excel表格最好是使用openpyxl进行操作 # encoding=utf-8 import time import xlrd import xlwt import openpyxl i

python对Excel表格操作

操作场景,给一个Excel表格随机生成10万个手机号码 python中常见的对Excel操作模块 xlwt module 将数据写入Excel表 xlrd module 读取Excel表格 xlsxwriter modole  xlwt模块的升级版,用于处理大量数据 先用xlwt写入Excel操作 import xlwt # 将数据写入Excel import random book = xlwt.Workbook(encoding='utf-8',style_compression=0) #

python编程:excel文件操作,redis数据库,接口开发

1.操作mysql import pymysql # 1.连上数据库 账号.密码 ip 端口号 数据库 #2.建立游标 #3.执行sql #4 .获取结果 # 5.关闭游标 #6.连接关闭 coon = pymysql.connect( host='数据库ip',user='jxz',passwd='123456', port=3306,db='jxz',charset='utf8' #port必须写int类型, #charset这里必须写utf8 ) cur = coon.cursor() #

【Python】excel读写操作 xlrd &amp; xlwt

xlrd ■ xlrd xlrd模块用于读取excel文件内容 基本用法: workbook = xlrd.open_workbook('文件路径') workbook.sheet_names() #返回所有sheet的列表 workbook.sheet_by_index(...) #通过index来获得一个sheet对象,index从0开始算起 workbook.sheet_by_name(...) #根据sheet名获得相应的那个sheet对象 在得到sheet对象之后,就可以用它的一些方法

Python对Excel的操作

Python几个读取Excel库的介绍: xlwings 可结合 VBA 实现对 Excel 编程,强大的数据输入分析能力,同时拥有丰富的接口,结合 pandas/numpy/matplotlib 轻松应对 Excel 数据处理工作. openpyxl 简单易用,功能广泛,单元格格式/图片/表格/公式/筛选/批注/文件保护等等功能应有尽有,图表功能是其一大亮点,缺点是对 VBA 支持的不够好. pandas 数据处理是 pandas 的立身之本,Excel 作为 pandas 输入/输出数据的容

python 对 excel 的操作

参考:https://www.php.cn/python-tutorials-422881.html  或 https://blog.51cto.com/wangfeng7399/2339556(使用openpyxl 操作) 1.python 操作 excel 的库:https://blog.51cto.com/wangfeng7399/2339556 (xlrd/xlwt.openpyxl) a.xlrd 操作的是 xls/xlxs 格式的 excel b.openpyxl 只支持 xlxs

python之excel表格操作

# coding: utf-8 #==================================================================================# Author: changbo - EMail:[email protected] #Last modified:2017-4-8 #filename:run_status.py #Description:create file 952700000ACH5Q05运行情况, Base xlswrit

基于.NET的Excel开发:单元格区域的操作(读取、赋值、边框和格式)

引用 using Excel = Microsoft.Office.Interop.Excel; 定义 1 Excel.ApplicationClass app; 2 Excel.Workbooks books; 3 Excel.Workbook book; 4 Excel.Sheets sheets; 5 Excel.Worksheet sheet; 6 Excel.Range m_objRange; 7 object missing = System.Reflection.Missing.V