Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - error on 'get_sheet_by_name'

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

like image 875
Doug C Avatar asked Mar 07 '18 18:03

Doug C


2 Answers

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

like image 149
Patrick Artner Avatar answered Oct 18 '22 20:10

Patrick Artner


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
like image 26
RonR Avatar answered Oct 18 '22 19:10

RonR