python中xlrd模块的使用(excel表格的处理)

目录

xlrd模块

打开.xls格式的文档

获取.xls文档的列

打印第一行的数据

打印所有数据

将.xls格式的文档转换为 .txt格式的

日期读出来是数字进行格式化


xlrd模块

xlrd模块是用来处理 .xls 格式的文档

打开.xls格式的文档

import xlrd
data=xlrd.open_workbook('test.xls')
print(data)

sheet1=data.sheet_by_index(0)
nrows=sheet1.nrows           #行数
ncols=sheet1.ncols           #列数
print(nrows,ncols)

for iRow in range(1,nrows):         #依次打印出每行的每一个字段的数据
	for iCol in range(ncols):
		data=sheet1.cell_value(iRow,iCol)
		print(data)

##############################################
<xlrd.book.Book object at 0x000001BAC978E2B0>
6 8

获取.xls文档的列

import xlrd
data=xlrd.open_workbook('test.xls')
line=data.sheets()
print(line)           #打印所有的列
print(line[1])        #打印第2列


可以看到有三列
#############################################################################
[<xlrd.sheet.Sheet object at 0x000001BACDE2EE10>, <xlrd.sheet.Sheet object at 0x000001BACDC55860>, <xlrd.sheet.Sheet object at 0x000001BACD7692E8>]
<xlrd.sheet.Sheet object at 0x000001BACDC55860>

打印第一行的数据

import xlrd
data=xlrd.open_workbook('test.xls')
line=data.sheets()[0]
print(line.row_values(0))


################################
['教工号', '单位', '姓名']

打印所有数据

import xlrd
data=xlrd.open_workbook('test.xls')
line=data.sheets()[0]
rows=line.nrows
for i in range(rows):
    print(int(line.row_values(i)[0]))

将.xls格式的文档转换为 .txt格式的

import xlrd
datas=xlrd.open_workbook('test.xls')
allline=datas.sheets()     #数据中的所有列数
lines=len(allline)
line=datas.sheets()[0]   #选定第一列
rows=line.nrows          #数据中的列数
print("这个文档有%s列,有%s行"%(lines,rows))
try:
    with open("test.txt","w",encoding='utf-8') as f:
        for i in range(rows):
            for j in range(lines):
                data=line.row_values(i)[j]   
                f.write(str(data)) 
                f.write("  ")              #用空格隔开
            f.write("\n")                  #每一行加换行符
except Exception as e:
    print("异常对象的类型是:%s"%type(e))
    print("异常对象的内容是:%s"%e)
finally:
    f.close()

 日期读出来是数字进行格式化

我们现在有如下的文件

实际打印出来是这样的

读取excel表格的数据类型有6种:

  • 0 empty
  • 1 string
  • 2 number
  • 3 date
  • 4 boolean
  • 5 error
import xlrd
import os
import time
from datetime import datetime
from xlrd import xldate_as_tuple
wb=xlrd.open_workbook("test.xls")
sheet1=wb.sheet_by_index(0)
nrows=sheet1.nrows
ncols=sheet1.ncols

def isVaildDate(sDate):
    try:
        if ":" in sDate:
            time.strptime(sDate, "%Y-%m-%d %H:%M:%S")
        else:
            time.strptime(sDate, "%Y-%m-%d")
        return True
    except:
        return False

for iRow in range(0,nrows):
	for iCol in range(ncols):
		sCell=sheet1.cell_value(iRow,iCol)
		print(sCell)
		ctype=sheet1.cell(iRow,iCol).ctype
		print("ctype类型为:%s"%ctype)

		if ctype == 3:
			date = datetime(*xldate_as_tuple(sCell, 0))
			if len(str(sCell).split(".")[1])>1:
				cell = date.strftime('%Y-%m-%d %H:%M:%S')
			else:
				cell = date.strftime('%Y-%m-%d')
			print(cell)
		elif ctype == 1:
			if isVaildDate(sCell):
				t1 = time.strptime(sCell, "%Y-%m-%d")
				sDate = changeStrToDate(t1,"yyyy-mm-dd")
				print(sDate)
		print("*"*10)

发布了428 篇原创文章 · 获赞 1401 · 访问量 142万+

猜你喜欢

转载自blog.csdn.net/qq_36119192/article/details/105050435