Python开发 之 Python3读写Excel文件(较全)

Python3读写Excel文件

  • 1、Python中几种常用包比较
  • 2、用xlrd包读取Excel文件
    • 2.1、用法
      • 2.1.1、引用包
      • 2.1.2、打开文件
      • 2.1.3、获取你要打开的sheet文件
      • 2.1.4、获取指定单元格里面的值
      • 2.1.5、获取某行或者某列的值
      • 2.1.6、获取sheet的名称,行数,列数
    • 2.2、源码示例
  • 3、尽量不用xlwt包写入Excel文件
    • 3.1、原因
    • 3.2、如果写入过多,会报错
    • 3.3、源码示例
  • 4、用openpyxl包写入Excel文件
    • 4.1、用法
      • 4.1.1、引用包
      • 4.1.2、创建工作簿
      • 4.1.3、创建sheet
      • 4.1.4、设置每个单元格里面的值
      • 4.1.5、保存文件
    • 4.2、源码示例
  • 5、用xlsxwriter包写入Excel文件
    • 5.1、简介
    • 5.2、用法
      • 5.2.1、引用包
      • 5.2.2、创建工作簿
      • 5.2.3、创建sheet
      • 5.2.4、设置每个单元格里面的值
      • 5.2.5、关闭工作簿
    • 5.3、源码示例
  • 6、Github源码分享

1、Python中几种常用包比较

2、用xlrd包读取Excel文件

2.1、用法

2.1.1、引用包

import xlrd

  • 1
  • 1

2.1.2、打开文件

xlrd.open_workbook(r'/root/excel/chat.xls')
  • 1
  • 1

2.1.3、获取你要打开的sheet文件

# 获取所有sheet sheet_name = workbook.sheet_names()[0] # 根据sheet索引或者名称获取sheet内容 sheet = workbook.sheet_by_index(0) # sheet索引从0开始

  • 1
  • 2
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5

2.1.4、获取指定单元格里面的值

sheet.cell_value(第几行,第几列)
  • 1
  • 1

2.1.5、获取某行或者某列的值

# 获取整行和整列的值(数组) rows = sheet.row_values(1) # 获取第2行内容 cols = sheet.col_values(2) # 获取第3列内容

  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

2.1.6、获取sheet的名称,行数,列数

    # sheet的名称,行数,列数    print (sheet.name,sheet.nrows,sheet.ncols)
  • 1
  • 2
  • 1
  • 2

2.2、源码示例

import xlrdfrom datetime import date,datetimearrayNum = 6#array = {'L1':'','L2':'','L3':'','L4':'','Question':'','Answer':''}tables = []newTables = []def read_excel(): # 打开文件 workbook = xlrd.open_workbook(r'/root/chat.xls') # 获取所有sheet sheet_name = workbook.sheet_names()[0] # 根据sheet索引或者名称获取sheet内容 sheet = workbook.sheet_by_index(0) # sheet索引从0开始 # sheet = workbook.sheet_by_name('Sheet1') #print (workboot.sheets()[0]) # sheet的名称,行数,列数 print (sheet.name,sheet.nrows,sheet.ncols) # 获取整行和整列的值(数组) rows = sheet.row_values(1) # 获取第2行内容 # cols = sheet.col_values(2) # 获取第3列内容 print (rows) # print (cols) for rown in range(sheet.nrows): array = {'L1':'','L2':'','L3':'','L4':'','Question':'','Answer':''} array['L1'] = sheet.cell_value(rown,0) array['L2'] = sheet.cell_value(rown,1) array['L3'] = sheet.cell_value(rown,2) array['L4'] = sheet.cell_value(rown,3) array['Question'] = sheet.cell_value(rown,4) array['Answer'] = sheet.cell_value(rown,5) tables.append(array) print (len(tables)) #print (tables) #print (tables[5])if __name__ == '__main__': # 读取Excel read_excel(); print ('读取成功')

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46

3、尽量不用xlwt包写入Excel文件

3.1、原因

在xlwt中生成的xls文件最多能支持65536行数据。

3.2、如果写入过多,会报错

由于数据太多,会报这个错误:

ValueError: row index (65536)not an intin range(65536)错误
  • 1
  • 1

3.3、源码示例

# 1. 导入模块 import xlwtdef write_excel(): # 2. 创建Excel工作薄 myWorkbook = xlwt.Workbook() # 3. 添加Excel工作表 mySheet = myWorkbook.add_sheet('A Test Sheet') # 4. 写入数据 myStyle = xlwt.easyxf('font: name Times New Roman, color-index red, bold on', num_format_str='#,##0.00') #数据格式 mySheet.write(i, j, 1234.56, myStyle) mySheet.write(2, 0, 1) #写入A3,数值等于1 mySheet.write(2, 1, 1) #写入B3,数值等于1 mySheet.write(2, 2, xlwt.Formula('A3+B3')) #写入C3,数值等于2(A3+B3) #5. 保存 myWorkbook.save('excelFile.xls')if __name__ == '__main__': # 写入Excel write_excel(); print ('写入成功')

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

4、用openpyxl包写入Excel文件

4.1、用法

4.1.1、引用包

import openpyxl
  • 1
  • 1

4.1.2、创建工作簿

f = openpyxl.Workbook() #创建工作簿

  • 1
  • 1

4.1.3、创建sheet

    sheet1 = f.create_sheet()
  • 1
  • 1

4.1.4、设置每个单元格里面的值

for jkey in range(len(newTables)): jk = 1 for cT in range(arrayNum): jk = jkey + 1 if cT == 0: sheet1.cell(row=jk,column=cT+1).value='1' else: sheet1.cell(row=jk,column=cT+1).value='2'

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

4.1.5、保存文件

    f.save('chatPy.xlsx') #保存文件
  • 1
  • 1

4.2、源码示例

import openpyxl#写exceldef write_excel(): f = openpyxl.Workbook() #创建工作簿 sheet1 = f.create_sheet() #sheet1 = f.add_sheet(u'sheet1',cell_overwrite_ok=True) #创建sheet row0 = [u'L1',u'L2',u'L3',u'L4',u'问题',u'答案'] #生成第一行 #for i in range(len(row0)): # sheet1.cell(column=i,row=0).value='L1') #生成后续 for jkey in range(len(newTables)): jk = 1 for cT in range(arrayNum): jk = jkey + 1 if cT == 0: sheet1.cell(row=jk,column=cT+1).value='1' else: sheet1.cell(row=jk,column=cT+1).value='2' f.save('chatPy.xlsx') #保存文件if __name__ == '__main__': # 写入Excel write_excel(); print ('写入成功')

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32

5、用xlsxwriter包写入Excel文件

5.1、简介

于是我找到了xlsxwriter这个模块,它生成的文件后缀名为.xlsx,最大能够支持1048576行数据,16384列数据

5.2、用法

5.2.1、引用包

import xlsxwriter
  • 1
  • 1

5.2.2、创建工作簿

workbook = xlsxwriter.Workbook('demo1.xlsx')#创建一个excel文件

  • 1
  • 1

5.2.3、创建sheet

  worksheet = workbook.add_worksheet(u'sheet1')#在文件中创建一个名为TEST的sheet,不加名字默认为sheet1
  • 1
  • 1

5.2.4、设置每个单元格里面的值

worksheet.write(3,0,35.5)#第4行的第1列设置值为35.5

  • 1
  • 1

5.2.5、关闭工作簿

  workbook.close()
  • 1
  • 1

5.3、源码示例

import xlsxwriter#写exceldef write_excel(): workbook = xlsxwriter.Workbook('chat.xlsx')#创建一个excel文件 worksheet = workbook.add_worksheet(u'sheet1')#在文件中创建一个名为TEST的sheet,不加名字默认为sheet1 worksheet.set_column('A:A',20)#设置第一列宽度为20像素 bold= workbook.add_format({'bold':True})#设置一个加粗的格式对象 worksheet.write('A1','HELLO')#在A1单元格写上HELLO worksheet.write('A2','WORLD',bold)#在A2上写上WORLD,并且设置为加粗 worksheet.write('B2',U'中文测试',bold)#在B2上写上中文加粗 worksheet.write(2,0,32)#使用行列的方式写上数字32,35,5 worksheet.write(3,0,35.5)#使用行列的时候第一行起始为0,所以2,0代表着第三行的第一列,等价于A4 worksheet.write(4,0,'=SUM(A3:A4)')#写上excel公式 workbook.close()if __name__ == '__main__': # 写入Excel write_excel(); print ('写入成功')

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

6、Github源码分享

https://github.com/ShaShiDiZhuanLan/Demo_Excel_Python

如果觉得写得不错,帮忙给 github的这个项目 点个star呗

(0)

相关推荐