Python – VSCode – Excel 操作 on Windows 10 No.58

自己投資としてチャレンジしている内容を Blog で公開しています。今回は Windows 10 の環境で python を利用し Excel を操作します。Python は Visual Studio Code 上で実行します。

————————————
▼1. Visual Studio Code を利用して Python で Excel ファイルを操作
————————————
Visual Studio Code は 色々な言語を Windows, Linux, Mac 上で実行できる環境を提供しています。前回は Excel ファイルを操作する python コードを紹介します。Excel を扱うために、openpyxl のライブラリーを使います。Ref: openpyxl · PyPI

————————————
▼2. 事前準備
————————————
2-1. 以下のサイトから Windows 版の Visual Studio Code をダウンロードし、インストールします。Ref:Download Visual Studio Code – Mac, Linux, Windows

2-2. Excel ファイルを操作するため、openpyxl をインストールします。

  1. Terminal を表示するため、Visual Studio Code 上で Ctrl+Shift+` をクリックします。
  2. 次に openpyxl をインストールします。
> pip install openpyxl
	Collecting openpyxl
	  Downloading openpyxl-3.0.10-py2.py3-none-any.whl (242 kB)
	     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 242.1/242.1 KB 15.5 MB/s eta 0:00:00
	Collecting et-xmlfile
	  Downloading et_xmlfile-1.1.0-py3-none-any.whl (4.7 kB)
	Installing collected packages: et-xmlfile, openpyxl
Successfully installed et-xmlfile-1.1.0 openpyxl-3.0.10

2-3. サンプルの Excel ファイルをダウンロードします。
Financial Sample Excel workbook  “Financial Sample.xlsx” https://go.microsoft.com/fwlink/?LinkID=521962
今回はダウンロードした Excel ファイルを python コードがあるディレクトリ c:\vscode\pythoncode に置きます。

————————————
▼3. Excel ファイルの操作
————————————
3-1. ディレクトリを作成し、そのディレクトリで Visual Studio Code (VSCode) を起動します。

(例)
mkdir c:\vscode\pythoncode
cd c:\vscode\pythoncode
code .

3-2. exceloperation.py のコードでは以下を行います。

  1. Excel “FinancialSample.xlsx” ファイルのロード
  2. シート名確認
  3. シートのデータの列名と 1 行目を表示
  4. シートを別のシートにコピーし、別名で Excel ファイルを保存
# (例) exceloperation.py

from openpyxl import load_workbook
from openpyxl import Workbook

#1ファイルのロード
wb=load_workbook('FinancialSample.xlsx')

#2シート名確認
ws=wb['Sheet1']
ws=wb.active
print(f'The sheet name is {ws.title}')

# get all of the sheet
#for row in ws.iter_rows():
#    for cell in row:
#        print(cell.value, end=' ')
#    print() 

#3 シートのデータの列名と 1 行目を表示 (A2-P2 が実際1行名)
first_row=ws['A1':'P2']
for row in first_row:
    for cell in row:
        print(cell.value, end=' ')
    print()

#4 シートを別のシートにコピーし、別名で Excel ファイルを保存
ws2=wb.copy_worksheet(ws)
ws2.title='Sheet2'
print(wb.sheetnames)
wb.save("C://work/vscode/pythoncode/FinancialSample2.xlsx")
print('Saved to FinancialSample2.xlsx') 

3-3. 実行結果
Python コードの実行方法は以下の blog を参考にしてください。
Python – Visual Studio Code の利用 No.34 – 2021/07

(コードの実行結果)
The sheet name is Sheet1
Segment Country Product Discount Band Units Sold Manufacturing Price Sale Price Gross Sales Discounts  Sales COGS Profit Date Month Number Month Name Year 
Government Canada Carretera None 1618.5 3 20 32370 0 32370 16185 16185 2014-01-01 00:00:00 1 January 2014
['Sheet1', 'Sheet2']
Saved to FinancialSample2.xlsx

————————————
▼4. 参考情報
————————————
(1) openpyxl · PyPI
(2) Download Visual Studio Code – Mac, Linux, Windows
(3) Sample ‘FinancialSample.xlsx https://go.microsoft.com/fwlink/?LinkID=521962
(4) Python – Visual Studio Code の利用 No.34 – 2021/07

以上です。参考になりましたら幸いです。

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です