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']

zx485
  • 28,498
  • 28
  • 50
  • 59
Doug C
  • 79
  • 1
  • 1
  • 2
  • 2
    looks like a warning and not an error – usernamenotfound Mar 07 '18 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 Mar 07 '18 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 Mar 07 '18 at 18:54
  • can you paste the new code and traceback? – usernamenotfound Mar 07 '18 at 18:55
  • 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 Mar 07 '18 at 19:02
  • "I get a warning" isn't really a problem. "I get an error after changing my code because of a warning" _is_ a problem, but we need to see your changed code, and the error you get, and what you expected to happen; seeing your old code and the warning you got doesn't help. – abarnert Mar 07 '18 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 Mar 07 '18 at 19:12

4 Answers4

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

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
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
RonR
  • 11
  • 2
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)
MNA
  • 11
  • 1
  • 4
-1
 from openpyxl import load_workbook
 #Load in the workbook
 wb = load_workbook('YourFileName.xlsx')
 wb.sheetnames # all names 
Zeeshan
  • 11
  • 5