LoginSignup
0
1

More than 3 years have passed since last update.

【Python】読込むブック名とセルの値をパラメーターにする。①

Last updated at Posted at 2021-02-18

pythonを使用してExcelファイルの操作を勉強しています。
本日の気づき(復習)は、パラメーターに関してです。
pythonでExcelを操作するため、openpyxlというパッケージを使用しています。

後々レイアウトを変更したり、プログラムで読み込むブック名やセルの値を変えてしまうかもしれない時の対処法です。
なんだかちょっと実戦形式っぽくてワクワクしてます。

今回はこちらのブック「日報.xlsx」を、使用します。
image.png

コマンドライン引数

sysモジュールのargv属性を使ってコマンドライン引数を取得します。
詳しくはこちらをご確認ください。
https://docs.python.org/ja/3/library/sys.html

commad_arguments.py
import sys
from openpyxl import load_workbook

filename = sys.argv[1]
cellno = sys.argv[2]

wb = load_workbook(filename, read_only=True)
ws = wb.active

print(ws[cellno].value)
# 読み込むブック名とセル名を入力して実行
>python commad_arguments.py 日報.xlsx I1
-->hogehoge

argv属性はパラメターをリストで返すそうです。
ちなみに、「argv[0]」にはプログラム名が入ります。

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