0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【備忘録】OpenPyXL get_sheet_names()、get_sheet_by_name()のDeprecationWarning

Last updated at Posted at 2020-08-31

事象

import openpyxl

wb = openpyxl.load_workbook('example.xlsx')
sheet_names = wb.get_sheet_names()
print(sheet_names)

sheet = wb.get_sheet_by_name('Sheet1')
value = sheet.cell(row=1, column=1).value
print(value)

警告が表示される。

Call to deprecated function get_sheet_names
(Use wb.sheetnames).sheet_names = wb.get_sheet_names()```

```DeprecationWarning: Call to deprecated function get_sheet_by_name 
(Use wb[sheetname]).sheet = wb.get_sheet_by_name('Sheet1')```

# 解決策
警告文の***Use***に従って下記のように書き換える。
```sheet_names = wb.sheetnames```
```sheet = wb['Sheet1']```

```python
import openpyxl

wb = openpyxl.load_workbook('example.xlsx')
sheet_names = wb.sheetnames
print(sheet_names)

sheet = wb['Sheet1']
value = sheet.cell(row=1, column=1).value
print(value)

#まとめ
非推奨!という警告なので、使えないという訳ではない。でも、警告でたら直したくなるよね。

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?