LoginSignup
0
1

More than 1 year has passed since last update.

EXCEL  workbookからシートを取得する 非推奨コードの改良 退屈なことはpythonにやらせよう

Last updated at Posted at 2021-09-01

退屈なことpythonにやらせようの本から自分用の覚書に。

退屈なことはpythonにやらせよう 第12章 Excelシート
公式Github → https://github.com/oreilly-japan/automatestuff-ja

workbookからシートを取得する(p.302)

元のコード


import openpyxl
wb = openpyxl.load_workbook("example.xlsx")
wb.get_sheet_names()

sheet = wb.get_sheet_by_name("Sheet3")
sheet

こうやって書くと、動作はするが「非推奨です」というメッセージが出る。

メッセージ例
DeprecationWarning: Call to deprecated function get_sheet_names (Use wb.sheetnames).
DeprecationWarning: Call to deprecated function get_sheet_by_name (Use wb[sheetname]).

改良されたコード


import openpyxl
wb = openpyxl.load_workbook("examle.xlsx")
wb.sheetnames

sheet = wb["Sheet3"]
sheet

このように書くと非推奨が出ない。

まとめ

この『退屈なことはPythonにやらせよう』という本、なぜか動かないことが多い。
だから深追いせずに「こんなことができるんだなー」ということを感じるための分厚目の入門書みたいな感じが良いかも。
私はそうやって使ってます。

0
1
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
1