Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getting sheet names from openpyxl

I have a moderately large xlsx file (around 14 MB) and OpenOffice hangs trying to open it. I was trying to use openpyxl to read the content, following this tutorial. The code snippet is as follows:

 from openpyxl import load_workbook  wb = load_workbook(filename = 'large_file.xlsx', use_iterators = True)  ws = wb.get_sheet_by_name(name = 'big_data')  

The problem is, I don't know the sheet name, and Sheet1/Sheet2.. etc. didn't work (returned NoneType object). I could not find a documentation telling me How to get the sheet names for an xlsx files using openpyxl. Can anyone help me?

like image 797
rivu Avatar asked May 07 '14 20:05

rivu


People also ask

How do I view a specific sheet in openpyxl?

Read Specific Cells You can access their values by using dictionary-like access: sheet["A2"]. value . Alternatively, you can assign sheet["A2"] to a variable and then do something like cell. value to get the cell's value.

Is openpyxl faster than pandas?

Step 3: Load with Openpyxl Still slow but a tiny drop faster than Pandas. Openpyxl Documentation: Memory use is fairly high in comparison with other libraries and applications and is approximately 50 times the original file size.

How do I get cell value from openpyxl?

Program to read cell value using openpyxl Library in PythonStep1: Import the openpyxl library to the Python program. Step2: Load/Connect the Excel workbook to the program. Step3: Get the title of the default first worksheet of the Workbook. Step4: Create variables and initialize them with the cell names.


1 Answers

Use the sheetnames property:

sheetnames

Returns the list of the names of worksheets in this workbook.

Names are returned in the worksheets order.

Type: list of strings

print (wb.sheetnames) 

You can also get worksheet objects from wb.worksheets:

ws = wb.worksheets[0] 
like image 113
alecxe Avatar answered Sep 25 '22 20:09

alecxe