python openpyxlエラー「DeprecationWarning: Call to deprecated function get_sheet_names (Use wb.sheetnames)」の対処法

python openpyxlエラー「DeprecationWarning: Call to deprecated function get_sheet_names (Use wb.sheetnames)」の対処法

pythonで、ライブラリopenpyxlのget_sheet_namesを使用時にエラー「「DeprecationWarning: Call to deprecated function get_sheet_names (Use wb.sheetnames)」」が発生した場合の対処法。pythonのバージョンは3.8.5を使用してます。

環境

  • OS windows10 pro 64bit
  • python 3.8.5
  • openpyxl 3.0.5

エラー全文

以下のコードを実行時に発生

import openpyxl

wb = openpyxl.load_workbook("sample.xlsx")

wb.get_sheet_names()

エラー全文

DeprecationWarning: 
Call to deprecated function get_sheet_names (Use wb.sheetnames).
  wb.get_sheet_names()

原因

get_sheet_names()は非推奨になっているため

対処法

エラーメッセージ通り「wb.sheetnames」を使用する

import openpyxl

wb = openpyxl.load_workbook("sample.xlsx")

sheet = wb.sheetnames

print(sheet)