Malik
Malik

Reputation: 1

Need some help opening excel files in python

Just having a strange issue. I am new in python and while running the below code. Geetting error. I have tried google but unable to run my code. Any advise please

import openpyxl
import os

os.chdir('/Users/omer/Documents/Python_Code/Udemy/Excel_Word_Pdf/')
workbook = openpyxl.load_workbook('example.xlsx')

sheet = workbook.get_sheet_by_name('Sheet1')

workbook.get_sheet_names()

cell = sheet['A1']

And the error i amgetting is

lesson42.py:13: DeprecationWarning: Call to deprecated function get_sheet_by_name (Use wb[sheetname]).
  sheet = workbook.get_sheet_by_name('Sheet1')
lesson42.py:15: DeprecationWarning: Call to deprecated function get_sheet_names (Use wb.sheetnames).
  workbook.get_sheet_names()

Upvotes: 0

Views: 186

Answers (3)

Teejay Bruno
Teejay Bruno

Reputation: 2169

Try viewing sheetnames first: workbook.sheetnames

This will give you a list of available sheets, then you can call them with workbook[#Some Sheet]

Upvotes: 0

Sri
Sri

Reputation: 2328

I just tested the following. This should work.

import openpyxl
import os

workbook = openpyxl.load_workbook('test.xlsx')

sheet = workbook['Sheet1']

print(workbook.sheetnames)

cell = sheet['A1'].value
print(cell)

Upvotes: 1

Michael Hsi
Michael Hsi

Reputation: 439

DeprecationWarning means that you're calling a function that's no longer supported. Go through their documentation to find the new function that's used to get_name, or try using pandas

Upvotes: 0

Related Questions