Python:Excel
阅读原文时间:2023年07月09日阅读:1

xlrd与xlwt:xls文件

读xlrd

写xlwt

import xlrd,xlwt

2.1 文件、表格信息的获取

打开文件:xlrd.open_workbook('Excel_name.xls')

workBook = xlrd.open_workbook('安徽省.xls')

表格名:workBook.sheet_names()

返回一个list类型,其中包含了所有sheet表的名字

allsheetname=workBook.sheet_names()
print(allsheetname)

['1-12', 'CNKI']

取其中一个表格的名字,以索引下标的方式

sheet1=allsheetname[0]

2.2读取

①获取整个sheet的内容并保存在变量中

按索引号:workBook.sheet_by_index(索引号)

按名字:workBook.sheet_by_name(名字)

#按索引号获取sheet内容
sheet1_content1=workBook.sheet_by_index(0)

#按sheet名字获取sheet内容
sheet1_content2=workBook.sheet_by_name('1-12')

②sheet的名字、行数、列数

三个属性:name、nrows、ncols

s1=sheet1_content1
print(s1.name,s1.nrows,s1.ncols)
1-12 516 7

③获取某行和某列的数据(存为list)

方法:

获取第i+1行:row_values(i)

获取第j+1列:col_values(j)

#获取第7行的内容
rows=sheet1_content1.row_values(6)

#获取第4列的内容
cols=sheet1_content1.col_values(3)

print(rows)
[' 行政区域面积', '平方公里', '1841', '2182', '1695', '2344', '2046']

④获取某个单元格的值

以获取14行5列为例

三种:

sheet1_content1.cell(13,4).value
sheet1_content1.cell_value(13,4)
sheet1_content1.row(13)[4].value

cell(a,b).value

cell_value(a,b)

row(a)[b].value

补充

循环Excel的所有行输出每个单元格值:

#循环表的每一行
for row in sheet1_content1.get_rows():
#循环一行的所有列
for col in row:
#获取每个单元格的值
print(col.value())

⑤单元格内容的数据类型

共6种:[0 empty , 1 string , 2 number , 3 date ,4 boolean , 5 error]

通过ctype属性访问

注意内容与④中值间的不同:内容是cell(13,4),值是cell(13,4).value

sheet1_content1.cell(13,4).ctype

1

完整的读代码:

import xlrd
def read_excel():

workBook = xlrd.open\_workbook('安徽省.xls')  
allsheetname = workBook.sheet\_names()  
# print(allsheetname)

sheet1 = allsheetname\[0\]  
# print(sheet1)

sheet1\_content1 = workBook.sheet\_by\_index(0)  
sheet1\_content2 = workBook.sheet\_by\_name('1-12')

rows = sheet1\_content1.row\_values(6)  
cols = sheet1\_content1.col\_values(4)

A1 = sheet1\_content1.cell(13, 4).value  
A2 = sheet1\_content1.cell\_value(13, 4)  
A3 = sheet1\_content1.row(13)\[4\].value

print(A1, A2, A3)

print(sheet1\_content1.cell(13,4).ctype)  

if __name__=='__main__':
read_excel()

①导入模块xlwt

import xlwt

②生成文件空间

xlwt.Workbook()

wb=xlwt.Workbook()

③给文件空间加表

add_sheet(表名)

sheet=wb.add_sheet('sheet1')

④向表中写入数据

sheet.write(row,col,内容)

for row in range(5):
for col in range(4):
sheet.write(row,col,'第{0}行第{1}列'.format(row,col))

⑤保存文件空间为文件

wb.save(文件名)

wb.save('test.xls')

完整代码:

import xlwt
def write_excel():
wb=xlwt.Workbook()
sheet=wb.add_sheet('sheet1')
for row in range(5):
for col in range(4):
sheet.write(row,col,'第{0}行第{1}列'.format(row+1,col+1))
wb.save('test.xls')

if __name__=='__main__':
write_excel()

1、读时的关键语句:

wb=xlrd.open_workbook(文件名) #打开文件空间

allsheetname=wb.sheet_names() #所有表名

sheet1=wb.sheet_by_index(索引) #通过索引打开表
sheet2=wb.sheet_by_name(表名) #通过名字打开表

rows=sheet1.row_values(6) #第7行的所有值
cols=sheet1.col_values(5) #第6列的所有值

#访问单个值
A1=sheet1.cell(6,5).value
A2=sheet1.cell_value(6,5)
A3=sheet1.row(6)[5].value

#循环访问所有值 此处是col.value而非sheet.cell().value
for row in sheet1.get_rows():
for col in row:
print(col.value)

2、写时的关键语句:

wb=xlwr.Workbook() #创建工作空间
sheet=wb.add_sheet('sheet1') #创建表 表名为sheet1

#通过循环行列添加数据
for row in range(5):
for col in range(4):
sheet.write(row,col,'第{0}行第{1}列'.format(row+1,col+1))

wb.save('test.xls') #保存为xls文件

3、表格属性

sheet的名字、行数、列数

三个属性:name、nrows、ncols

sheet.name
sheet.nrows
sheet.ncols

1、读取EXCEL,不带第一行的标题

Python:读取Excel 不带第一行标题

2、读取表格时,无法显示正确的汉字

EXCEL:汉字解码后输出

xlrd与xlwt处理的是xls文件,单个sheet最大的行数为65535,超过时报错ValueError: row index was 65536, not allowed by .xls format

当需要处理更多行数即xlsx时,可以使用openpyxl函数,最大行数为1048576。

import openpyxl

①加载xlsx文件

filename=r'D:\诗歌.xlsx'
wb=openpyxl.load_workbook(filename) #加载文件

②获取sheet内容

sheetnames = wb.get_sheet_names()#获取所有sheet的名字
sheet = wb.get_sheet_by_name(sheetnames[0])#以名字的方式访问sheet

#也可以直接以Key索引的方式访问
sheet = wb[ sheetnames[0] ]
#如果有多个表,可以用for循环遍历
#for sheet in wb:
  print(sheet.title)

③获取sheet属性

rows = sheet.max_row #最大行数
cols = sheet.max_colmn #最大列数

④获取某个单元格的值

sheet.cell(1,1).value

sheet['A1']

①打开workbook并添加sheet

outwb=openpyxl.Workbook() #创建一个Workbook
sheet =outwb.create_sheet(index=0) #在Workbook下创建sheet

②写入

for row in range(1,10):
for col in range(1,10):
sheet.cell(row,col).value='{}行{}列'.format(row,col)

#以上是主流方式
#还有其他两种方式
#①用Key索引方式 sheet['A1']='1行1列'
#②一次写一行,从原文件最左下方开始 sheet.append([1,2,3])
#方法②的结果是在原文件后多了1行3列,内容是1 2 3

③保存

out_filename='测试.xlsx'
outwb.save(out_filename)

1、openpyxl只能读写xlsx文件,不能读写xls文件;xlwt和xlrd正好相反;

2、openpyxl的读写起始行,在循环时都是从1开始,表示第i行;而xlwt与xlrd的起始行是0,实际表示第i+1行

3、写文件名时,要加r前缀,或者把字符串中的所有连接符\写为双斜杠\\

1、读的关键语句

filename=r'D:\词语.xlsx'
wb=openpyxl.load_workbook(filename) #①打开xlsx文件

sheetnames=wb.get_sheet_names() #②获取文件中的所有sheet名
ws=wb.get_sheet_by_name(sheetnames[0])#③打开第一个sheet

#④获取最大行数和最大列数
rows = ws.max_row
cols = ws.max_column

#⑤for循环读取单元格数据,起始下标为1
for r in range(1 , rows):
for c in range (1 , cols):
print( ws.cell(r,c).value )

2、写的关键语句

outwb=openpyxl.Workbook() #①新建workbook
ws=outwb.create_sheet(index=0) #②在之前的workbook中新建一个sheet

#③用for循环为每个单元格赋值,range(1,len+1),len为要写入的行/列数
#下文便是10行10列
for r in range(1,11):
for c in range(1,11):
ws.cell(r,c).value='{}行{}列'.format(r,c)

#④保存为文件
out_filename='测试.xlsx'
outwb.save( out_filename )

学习自:

python 读写 Excel文件 - shaomine - 博客园

浅谈Python_Openpyxl使用(最全总结) - 吾爱源码