Pandas: Looking up the list of sheets in an excel file
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
and get $2,000 discount on your first invoice
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: Hypnotic Puzzle2
--
Chapters
00:00 Pandas: Looking Up The List Of Sheets In An Excel File
00:31 Accepted Answer Score 515
00:47 Answer 2 Score 95
01:18 Answer 3 Score 15
01:38 Answer 4 Score 24
01:59 Thank you
--
Full question
https://stackoverflow.com/questions/1797...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #excel #pandas #openpyxl #xlrd
#avk47
ACCEPTED ANSWER
Score 515
You can still use the ExcelFile class (and the sheet_names attribute):
xl = pd.ExcelFile('foo.xls')
xl.sheet_names # see all sheet names
xl.parse(sheet_name) # read a specific sheet to DataFrame
see docs for parse for more options...
ANSWER 2
Score 95
You should explicitly specify the second parameter (sheet_name) as None. like this:
df = pandas.read_excel("/yourPath/FileName.xlsx", sheet_name=None);
"df" are all sheets as a dictionary of DataFrames, you can verify it by run this:
df.keys()
result like this:
[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']
please refer pandas doc for more details: https://pandas.pydata.org/pandas-docs/stable/generated/pandas.read_excel.html
ANSWER 3
Score 24
The easiest way to retrieve the sheet-names from an excel (xls., xlsx) is:
tabs = pd.ExcelFile("path").sheet_names
print(tabs)
Then to read and store the data of a particular sheet (say, sheet names are "Sheet1", "Sheet2", etc.), say "Sheet2" for example:
data = pd.read_excel("path", "Sheet2")
print(data)
ANSWER 4
Score 15
This is the fastest way I have found, inspired by @divingTobi's answer. All The answers based on xlrd, openpyxl or pandas are slow for me, as they all load the whole file first.
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")]