新版Pandas使用以下界面加载Excel文件:

read_excel('path_to_file.xls', 'Sheet1', index_col=None, na_values=['NA'])

但如果我不知道有哪些床单呢?

例如,我正在工作的excel文件,如下表

数据1,数据2…,数据N, foo, bar

但我不知道先验的N。

有没有办法从熊猫的excel文档中获得表的列表?


当前回答

您应该显式地将第二个参数(sheetname)指定为None。是这样的:

 df = pandas.read_excel("/yourPath/FileName.xlsx", None);

"df"都是一个数据帧字典,你可以通过运行这个来验证:

df.keys()

结果是这样的:

[u'201610', u'201601', u'201701', u'201702', u'201703', u'201704', u'201705', u'201706', u'201612', u'fund', u'201603', u'201602', u'201605', u'201607', u'201606', u'201608', u'201512', u'201611', u'201604']

详情请参考熊猫文档:https://pandas.pydata.org/pandas-docs/stable/generated/pandas.read_excel.html

其他回答

您应该显式地将第二个参数(sheetname)指定为None。是这样的:

 df = pandas.read_excel("/yourPath/FileName.xlsx", None);

"df"都是一个数据帧字典,你可以通过运行这个来验证:

df.keys()

结果是这样的:

[u'201610', u'201601', u'201701', u'201702', u'201703', u'201704', u'201705', u'201706', u'201612', u'fund', u'201603', u'201602', u'201605', u'201607', u'201606', u'201608', u'201512', u'201611', u'201604']

详情请参考熊猫文档:https://pandas.pydata.org/pandas-docs/stable/generated/pandas.read_excel.html

import pandas as pd

path = "\\DB\\Expense\\reconcile\\"

file_name = "202209-v01.xlsx"

df = pd.read_excel(path + file_name, None)
print(df)

sheet_names = list(df.keys())

# print last sheet name
print(sheet_names[len(sheet_names)-1])

last_month = df.get(sheet_names[len(sheet_names)-1])
print(last_month)

With the load_workbook readonly option, what was earlier seen as a execution seen visibly waiting for many seconds happened with milliseconds. The solution could however be still improved. import pandas as pd from openpyxl import load_workbook class ExcelFile: def __init__(self, **kwargs): ........ ..... self._SheetNames = list(load_workbook(self._name,read_only=True,keep_links=False).sheetnames) The Excelfile.parse takes the same time as reading the complete xls in order of 10s of sec. This result was obtained with windows 10 operating system with below package versions C:\>python -V Python 3.9.1 C:\>pip list Package Version --------------- ------- et-xmlfile 1.0.1 numpy 1.20.2 openpyxl 3.0.7 pandas 1.2.3 pip 21.0.1 python-dateutil 2.8.1 pytz 2021.1 pyxlsb 1.0.8 setuptools 49.2.1 six 1.15.0 xlrd 2.0.1

基于@dhwanil_shah的回答,您不需要提取整个文件。zf。打开它可以直接从压缩文件中读取。

import xml.etree.ElementTree as ET
import zipfile

def xlsxSheets(f):
    zf = zipfile.ZipFile(f)

    f = zf.open(r'xl/workbook.xml')

    l = f.readline()
    l = f.readline()
    root = ET.fromstring(l)
    sheets=[]
    for c in root.findall('{http://schemas.openxmlformats.org/spreadsheetml/2006/main}sheets/*'):
        sheets.append(c.attrib['name'])
    return sheets

连续的两个readline很难看,但是内容只在文本的第二行。不需要解析整个文件。

这个解决方案似乎比read_excel版本快得多,而且很可能也比完整的提取版本快得多。

如果你:

关注业绩 在执行时不需要文件中的数据。 想要使用传统的库还是使用自己的解决方案

下面是一个~10Mb的xlsx, xlsb文件的基准测试。

xls, xlsx

from openpyxl import load_workbook

def get_sheetnames_xlsx(filepath):
    wb = load_workbook(filepath, read_only=True, keep_links=False)
    return wb.sheetnames

基准测试:~ 14倍的速度提升

# get_sheetnames_xlsx vs pd.read_excel
225 ms ± 6.21 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
3.25 s ± 140 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

xlsb

from pyxlsb import open_workbook

def get_sheetnames_xlsb(filepath):
  with open_workbook(filepath) as wb:
     return wb.sheets

基准测试:~ 56倍的速度提升

# get_sheetnames_xlsb vs pd.read_excel
96.4 ms ± 1.61 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
5.36 s ± 162 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

注:

这是一个很好的资源 http://www.python-excel.org/ XLRD从2020年起不再维持