Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python openpyxl get sheet names

I am starting on a code that loads and edits excel (the version I am using is office 2017) sheet using openpyxl. Right now I am still trying to wrap my head around how this module works, here's the code

import openpyxl
from openpyxl import load_workbook
from openpyxl import workbook
from openpyxl.compat import range
from openpyxl.utils import get_column_letter
import os
os.chdir("D:\Scripts\Python\Testing Scripts\My Excel Folder")

wb = load_workbook("MyExcel.xlsx")
names = wb.sheetnames()

print(names)
print(type(wb))

and the error I receive is,

TypeError: 'list' object is not callable

For the string of code

names = wb.sheetnames()

like image 607
Nickiel Avatar asked Dec 03 '25 23:12

Nickiel


1 Answers

wb.get_sheet_names() returns the list of all the sheets in that excel workbook.

print (wb.get_sheet_names())

for the latest openpyxl to avoid warning:

print (wb.sheetnames)

if you want to access a particular sheet

 ws = wb.get_sheet_by_name(name = 'Sheet 1') 
like image 172
ReKx Avatar answered Dec 06 '25 15:12

ReKx