7

This is my code so far:

import os
import openpyxl
os.chdir('C:\\Python34\\MyPy')
wb=openpyxl.load_workbook('example.xlsx')
wb.get_sheet_names()

But I get these errors:

Warning (from warnings module):
/File "main", line 1
DeprecationWarning: Call to deprecated function get_sheet_names (Use wb.sheetnames).
['Sheet1', 'Sheet2', 'Sheet3']

7
  • 2
    looks like a warning and not an error Commented Mar 7, 2018 at 18:52
  • Hi all, I get an error due to using 'get_sheet_by_name' when I write the above code. I tried using the 'sheetnames' function instead as mentioned in the error, but can't get it to work. Anyone?? thanks..
    – Doug C
    Commented Mar 7, 2018 at 18:52
  • True, it's a warning, but nothing seems to work after that. Using 'wb.sheetnames()' instead of get_sheet_names gives another error
    – Doug C
    Commented Mar 7, 2018 at 18:54
  • 1
    Ah, think I figured it out.... putting 'wb.sheetnames' on the next line works. I entered 'wb.sheetnames()' before - doesn't like the (). Thx Usernamenotfound
    – Doug C
    Commented Mar 7, 2018 at 19:02
  • 1
    Thank you for the interesting question. However, If the warning is not the problem -- please ask a specif question as to what the real problem is. What is it not doing that it should? "It doesn't work" does not give the reader enough information to answer the question.
    – SteveJ
    Commented Mar 7, 2018 at 19:12

4 Answers 4

16

Warnings aren't errors - they won't hinder your program from running. In case of deprecated warnings: you use a feature that will be removed in future versions so the devs lable it deprecated.

It may work now, but next version it may no longer as this feature was removed - then you will get an error.

You can fix it like this:

wb.sheetnames # all names

sheet = wb["UseThisSheet"]  # select a certain sheet by name

for sheet2 in wb:           # or go over all sheets 
    print(sheet2.title)

sh = wb.active             # normally sheet at index 0 if you create an empy one

Source: https://openpyxl.readthedocs.io/en/stable/tutorial.html

Here is a short example on how to create a workbook (my answer to some other xlsx question): https://stackoverflow.com/a/48782382/7505395

1

Since I bounce around in a few different worksheets in my python, I needed the whole list to work from. I added the following

wbInputFile = load_workbook(inFile)
sheetList = wbInputFile.sheetnames
worksheet = wbInputFile[ sheetList[0] ]

#  now I am able to run through the rows and get the info
for row in range(2,worksheet.max_row ):
    # get the values for each cell needed
0

It gives a warning , but the below code works fine (using Python3 ).I tried with wb.sheetnames instead of wb.get_sheet_names()

import openpyxl
path="C:\\Users\user1\PycharmProjects\Projectstatus\My Python.xlsx"
wb=openpyxl.load_workbook(path)
print(wb.sheetnames)
-1
 from openpyxl import load_workbook
 #Load in the workbook
 wb = load_workbook('YourFileName.xlsx')
 wb.sheetnames # all names 

Not the answer you're looking for? Browse other questions tagged or ask your own question.