新版Pandas使用以下界面加载Excel文件:
read_excel('path_to_file.xls', 'Sheet1', index_col=None, na_values=['NA'])
但如果我不知道有哪些床单呢?
例如,我正在工作的excel文件,如下表
数据1,数据2…,数据N, foo, bar
但我不知道先验的N。
有没有办法从熊猫的excel文档中获得表的列表?
新版Pandas使用以下界面加载Excel文件:
read_excel('path_to_file.xls', 'Sheet1', index_col=None, na_values=['NA'])
但如果我不知道有哪些床单呢?
例如,我正在工作的excel文件,如下表
数据1,数据2…,数据N, foo, bar
但我不知道先验的N。
有没有办法从熊猫的excel文档中获得表的列表?
当前回答
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
其他回答
这是我发现的最快的方法,灵感来自@divingTobi的答案。所有基于xlrd、openpyxl或pandas的答案对我来说都很慢,因为它们都先加载整个文件。
from zipfile import ZipFile
from bs4 import BeautifulSoup # you also need to install "lxml" for the XML parser
with ZipFile(file) as zipped_file:
summary = zipped_file.open(r'xl/workbook.xml').read()
soup = BeautifulSoup(summary, "xml")
sheets = [sheet.get("name") for sheet in soup.find_all("sheet")]
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
#It will work for Both '.xls' and '.xlsx' by using pandas
import pandas as pd
excel_Sheet_names = (pd.ExcelFile(excelFilePath)).sheet_names
#for '.xlsx' use only openpyxl
from openpyxl import load_workbook
excel_Sheet_names = (load_workbook(excelFilePath, read_only=True)).sheet_names
from openpyxl import load_workbook
sheets = load_workbook(excel_file, read_only=True).sheetnames
对于我正在使用的5MB Excel文件,没有read_only标志的load_workbook花了8.24秒。对于read_only标志,只需要39.6 ms。如果您仍然希望使用Excel库而不使用xml解决方案,那么这比解析整个文件的方法要快得多。
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)