ValueError: row index was 65536, not allowed by .xls format

报错:ValueError: row index was 65536, not allowed by .xls formatpython

 

读取.xls文件正常,在写.xls文件,pd.to_excel()时候会报错函数

 

缘由:写入的文件行数大于65536excel

 

Pandas 读取 Excel 文件的引擎是 xlrd ,xlrd 虽然同时支持 .xlsx 和 .xls 两种文件格式,可是在源码文件 xlrd/sheet.py 中限制了读取的 Excel 文件行数必须小于 65536,列数必须小于 256。orm

 

xlrd和xlwt处理的是xls文件,单个sheet最大行数是65535,若是有更大须要的,建议使用openpyxl函数,最大行数达到1048576。
若是数据量超过65535就会遇到:ValueError: row index was 65536, not allowed by .xls formatblog

 

解决:get

方法1: 直接使用openpyxl源码

import openpyxl

def readExel():
    filename = r'D:\test.xlsx'
    inwb = openpyxl.load_workbook(filename)  # 读文件
    sheetnames = inwb.get_sheet_names()  # 获取读文件中全部的sheet,经过名字的方式
    ws = inwb.get_sheet_by_name(sheetnames[0])  # 获取第一个sheet内容

    # 获取sheet的最大行数和列数
    rows = ws.max_row
    cols = ws.max_column
    for r in range(1,rows):
        for c in range(1,cols):
            print(ws.cell(r,c).value)
        if r==10:
            break

def writeExcel():
    outwb = openpyxl.Workbook()  # 打开一个将写的文件
    outws = outwb.create_sheet(index=0)  # 在将写的文件建立sheet
    for row in range(1,70000):
        for col in range(1,4):
            outws.cell(row, col).value = row*2  # 写文件
        print(row)
    saveExcel = "D:\\test2.xlsx"
    outwb.save(saveExcel)  # 必定要记得保存

方法2:Pandas 的 read_excel 方法中,有 engine 字段,能够指定所使用的处理 Excel 文件的引擎,填入 openpyxl ,再读取文件就能够了。pandas

import pandas as pd

df = pd.read_excel(‘./data.xlsx’, engine=’openpyxl’)  pd.write( ,engine=’openpyxl’)
相关文章
相关标签/搜索