(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:
# print
# 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)
print
'[' + ','.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种类型的更多相关文章

  1. Python读取excel中的图片

    作为Java程序员,Java自然是最主要的编程语言.但是Java适合完成大型项目,对于平时工作中小的工作任务,需要快速完成,易于修改和调试,使用Java显得很繁琐,需要进行类的设计,打成jar包,出现 ...

  2. excel 截取单元格部分内容(从指定位置截取)

      excel 截取单元格部分内容(从指定位置截取) CreateTime--2018年5月28日08:28:46 Author:Marydon 1.情景展示 截取手机号后6位 2.实现 语法说明:r ...

  3. 读取Excel,单元格内容大于255个字符自动被截取的问题

    DataSet ds = new DataSet(); cl_initPage.v_DeBugLog("ExcelDataSource进入"); string strConn; s ...

  4. Python 实现 Excel 里单元格的读写与清空操作

    #coding=utf-8 # coding=utf-8 作用是声明python代码的文本格式是utf-8,python按照utf-8的方式来读取程序. # 如果不加这个声明,无论代码中还是注释中有中 ...

  5. VBS读取txt文档数据查找Excel中单元格数据符合条件的剪切到工作表2中

    Dim fso,f,a set oExcel = CreateObject( "Excel.Application" ) oExcel.Visible = false '4) 打开 ...

  6. Python 用load_workbook 读取excel某个单元格数据、读取excel行数、列数

    from openpyxl import load_workbook path = r'D:\pywork\12' # EXCEL信息所在文件夹 e= load_workbook(path + '/' ...

  7. 用OLEDB读取EXCEL时,单元格内容长度超过255被截断

    https://support.microsoft.com/zh-cn/help/189897/data-truncated-to-255-characters-with-excel-odbc-dri ...

  8. excel中单元格计算

    首先,得明确excel中相对引用和绝对引用的概念,这里$符号起着关键作用,当在一个行或列的指示符前面加$则表示绝对引用,否则相对引用,具体: 1.相对引用,复制公式时地址跟着发生变化,如C1单元格有公 ...

  9. 使用net core 6 c# 的 NPOI 包,读取excel..xlsx单元格内的图片,并存储到指定服务器

    这个是记录,单元格的图片. 直接上代码,直接新建一个 net core api 解决方案,引用一下nuget包.本地创建一个 .xlsx 格式的excel文件 using ICSharpCode.Sh ...

随机推荐

  1. jquery中选取兄弟节点的方法

    $('#id').siblings() 当前元素所有的兄弟节点$('#id').prev() 当前元素前一个兄弟节点$('#id').prevaAll() 当前元素之前所有的兄弟节点$('#id'). ...

  2. Python开发【笔记】:PEP 8 编码规范

    PEP 8 编码规范     https://bk.tencent.com/document/bkrule/040101.pdf                                     ...

  3. python QT 编程之路

    pyQT4  的Wheel 下载 https://www.lfd.uci.edu/~gohlke/pythonlibs/#pyqt4 python发送GET 或者 POST请求 https://www ...

  4. python server

    #!/usr/bin/env python #coding=utf-8 # modifyDate: 20120808 ~ 20120810 # 原作者为:bones7456, http://li2z. ...

  5. hotplug 热拔插机制框架

    框架入口源文件: mdev.c (可根据入口源文件,再按着框架到内核走一遍) 内核版本:linux_2.6.22.6     硬件平台:JZ2440 以下是驱动框架:

  6. json和jsonp的区别?

    json返回的是一串json格式数据:而jsonp返回的是脚本代码(包含一个函数调用): jsonp的全名叫做json with padding,就是把 json 对象用符合 js 语法的形式包裹起来 ...

  7. 日志监控工具安装:windows上安装elk

    Elasticsearch + Kibana + logstash   for     windows   安装 https://blog.csdn.net/u011781521/article/de ...

  8. sqlserver数据库查询,在数据类型不一致时容易出错

    1. 如此句sql: select SysNo from User_MainInfo where Ouid=@Ouid 在 User_MainInfo表中Ouid是nvarchar类型,但当我们传入的 ...

  9. windows系统redmine安装总结

    今天在公司服务器上安装了redmine,主要用于项目管理和缺陷跟踪.安装过程比较简单,总结如下: 1.网上下载redmine安装包(bitnami-redmine-3.3.1-0-windows-in ...

  10. 【分类器】感知机+线性回归+逻辑斯蒂回归+softmax回归

    一.感知机     详细参考:https://blog.csdn.net/wodeai1235/article/details/54755735 1.模型和图像: 2.数学定义推导和优化: 3.流程 ...