python读取excel中单元格的内容返回的5种类型
(1) 读取单个sheetname的内容。
此部分转自:https://www.cnblogs.com/xxiong1031/p/7069006.html
python读取excel中单元格的内容返回的有5种类型,即上面例子中的ctype:
ctype:
0 empty
1 string
2 number
3 date
4 boolean
5 Error
# coding=utf-8
import xlrd
import sys reload(sys)
sys.setdefaultencoding('utf-8')
import traceback
from datetime import datetime
from xlrd import xldate_as_tuple class excelHandle:
def decode(self, filename, sheetname):
try:
filename = filename.decode('utf-8')
sheetname = sheetname.decode('utf-8')
except Exception:
print traceback.print_exc()
return filename, sheetname def read_excel(self, filename, sheetname):
filename, sheetname = self.decode(filename, sheetname)
rbook = xlrd.open_workbook(filename)
sheet = rbook.sheet_by_name(sheetname)
rows = sheet.nrows
cols = sheet.ncols
all_content = []
for i in range(rows):
row_content = []
for j in range(cols):
ctype = sheet.cell(i, j).ctype # 表格的数据类型
cell = sheet.cell_value(i, j)
if ctype == 2 and cell % 1 == 0: # 如果是整形
cell = int(cell)
elif ctype == 3:
# 转成datetime对象
date = datetime(*xldate_as_tuple(cell, 0))
cell = date.strftime('%Y/%d/%m %H:%M:%S')
elif ctype == 4:
cell = True if cell == 1 else False
row_content.append(cell)
all_content.append(row_content)
print '[' + ','.join("'" + str(element) + "'" for element in row_content) + ']'
return all_content if __name__ == '__main__':
eh = excelHandle()
filename = r'G:\test\ctype.xls'
sheetname = 'Sheet1'
eh.read_excel(filename, sheetname)
(2) 稍微修改了一下,读取excel所有sheets的内容。
# coding=utf-8
import xlrd
import sys # reload(sys)
# sys.setdefaultencoding('utf-8')
import traceback
from datetime import datetime
from xlrd import xldate_as_tuple class excelHandle:
# def decode(self, filename, sheetname):
# try:
# filename = filename.decode('utf-8')
# sheetname = sheetname.decode('utf-8')
# except Exception:
# traceback.print_exc()
# return filename, sheetname def read_excel(self, filename):
# filename, sheetname = self.decode(filename, sheetname)
rbook = xlrd.open_workbook(filename)
sheets = rbook.sheet_names() # 获取所有sheet名
allSheetsContent=[]
for sh in sheets:
sheet = rbook.sheet_by_name(sh)
rows = sheet.nrows
cols = sheet.ncols
sheetContent = []
for i in range(1,rows):
rowContent = []
for j in range(cols):
ctype = sheet.cell(i, j).ctype # 表格的数据类型
cell = sheet.cell_value(i, j)
if ctype == 2 and cell % 1 == 0: # 如果是整形
cell = int(cell)
elif ctype == 3:
# 转成datetime对象
date = datetime(*xldate_as_tuple(cell, 0))
cell = date.strftime('%Y/%d/%m %H:%M:%S')
elif ctype == 4:
cell = True if cell == 1 else False
rowContent.append(cell)
allSheetsContent.append(rowContent)
# sheetContent.append(rowContent)
# print('[' + ','.join("'" + str(element) + "'" for element in rowContent) + ']') return allSheetsContent def read_excel_by_sheetname(self, filename, sheetname):
filename, sheetname = self.decode(filename, sheetname)
rbook = xlrd.open_workbook(filename)
sheet = rbook.sheet_by_name(sheetname)
rows = sheet.nrows
cols = sheet.ncols
all_content = []
for i in range(rows):
row_content = []
for j in range(cols):
ctype = sheet.cell(i, j).ctype # 表格的数据类型
cell = sheet.cell_value(i, j)
if ctype == 2 and cell % 1 == 0: # 如果是整形
cell = int(cell)
elif ctype == 3:
# 转成datetime对象
date = datetime(*xldate_as_tuple(cell, 0))
cell = date.strftime('%Y/%d/%m %H:%M:%S')
elif ctype == 4:
cell = True if cell == 1 else False
row_content.append(cell)
all_content.append(row_content)
'[' + ','.join("'" + str(element) + "'" for element in row_content) + ']'
return all_content if __name__ == '__main__':
eh = excelHandle()
filename = r'E:\Code\subject.xlsx'
all_content = eh.read_excel(filename)
for a in all_content:
print(a)
python读取excel中单元格的内容返回的5种类型的更多相关文章
- Python读取excel中的图片
作为Java程序员,Java自然是最主要的编程语言.但是Java适合完成大型项目,对于平时工作中小的工作任务,需要快速完成,易于修改和调试,使用Java显得很繁琐,需要进行类的设计,打成jar包,出现 ...
- excel 截取单元格部分内容(从指定位置截取)
excel 截取单元格部分内容(从指定位置截取) CreateTime--2018年5月28日08:28:46 Author:Marydon 1.情景展示 截取手机号后6位 2.实现 语法说明:r ...
- 读取Excel,单元格内容大于255个字符自动被截取的问题
DataSet ds = new DataSet(); cl_initPage.v_DeBugLog("ExcelDataSource进入"); string strConn; s ...
- Python 实现 Excel 里单元格的读写与清空操作
#coding=utf-8 # coding=utf-8 作用是声明python代码的文本格式是utf-8,python按照utf-8的方式来读取程序. # 如果不加这个声明,无论代码中还是注释中有中 ...
- VBS读取txt文档数据查找Excel中单元格数据符合条件的剪切到工作表2中
Dim fso,f,a set oExcel = CreateObject( "Excel.Application" ) oExcel.Visible = false '4) 打开 ...
- Python 用load_workbook 读取excel某个单元格数据、读取excel行数、列数
from openpyxl import load_workbook path = r'D:\pywork\12' # EXCEL信息所在文件夹 e= load_workbook(path + '/' ...
- 用OLEDB读取EXCEL时,单元格内容长度超过255被截断
https://support.microsoft.com/zh-cn/help/189897/data-truncated-to-255-characters-with-excel-odbc-dri ...
- excel中单元格计算
首先,得明确excel中相对引用和绝对引用的概念,这里$符号起着关键作用,当在一个行或列的指示符前面加$则表示绝对引用,否则相对引用,具体: 1.相对引用,复制公式时地址跟着发生变化,如C1单元格有公 ...
- 使用net core 6 c# 的 NPOI 包,读取excel..xlsx单元格内的图片,并存储到指定服务器
这个是记录,单元格的图片. 直接上代码,直接新建一个 net core api 解决方案,引用一下nuget包.本地创建一个 .xlsx 格式的excel文件 using ICSharpCode.Sh ...
随机推荐
- python循环字符转换
pyhon函数传参的时候穿的是引用,而不是实际值,这样可以节省内存 变量名要求:最好是以字母下划线作为变量名,不能和py关键字重复 import getpass提供了平台无关的在命令行下输入密码的方法 ...
- php面向对象的封装性
面向对象编程 1:封装性 访问修饰符,作用为封装,防止外部访问. public 公有的 private 私有的 protected 受保护的 一开始具体也没搞明白是怎么回事,搞个小的Demo就出来了 ...
- # 20165225 《Java程序设计》第一周学习总结
20165225 <Java程序设计>第一周学习总结 1.视频与课本中的学习: 首先是为了运行和开发Java分别安装了JRE和JDK,具体做法在老师给的<Java2 实用教程(第五版 ...
- Apache Spark支持三种分布式部署方式 standalone、spark on mesos和 spark on YARN区别
链接地址: http://dongxicheng.org/framework-on-yarn/apache-spark-comparing-three-deploying-ways/ Spark On ...
- Linux下Redis的安装与启动
一. 进入目录(我们准备将redis装入opt文件夹) $ cd /opt/ 二.下载redis压缩包 $ wget http://download.redis.io/releases/redis-4 ...
- MySQL中varchar最大长度是多少?
一. varchar存储规则: 4.0版本以下,varchar(20),指的是20字节,如果存放UTF8汉字时,只能存6个(每个汉字3字节) 5.0版本以上,varchar(20),指的是20字符,无 ...
- Python3学习之路~4.2 迭代器
可以直接作用于for循环的数据类型有以下几种: 一类是集合数据类型,如list.tuple.dict.set.str等: 一类是generator,包括生成器和带yield的generator fun ...
- abap function module中的异常处理
1: 定义一个有异常抛出的function module. (zfm_moudle6), 该函数中有符合exceptions中的异常,将会自动将exceptions中的异常抛出. FUNCTION ...
- 339A
#include <iostream> #include <string> #include <algorithm> using namespace std; #d ...
- 深入理解Lua的闭包一:概念、应用和实现原理
本文首先通过具体的例子讲解了Lua中闭包的概念,然后总结了闭包的应用场合,最后探讨了Lua中闭包的实现原理. 闭包的概念 在Lua中,闭包(closure)是由一个函数和该函数会访问到的非局部变量 ...