LoginSignup
0
0

More than 1 year has passed since last update.

OpenPyXlでExcelを転記するコード(基本)

Last updated at Posted at 2022-02-13

OpenPyXlでExcelからExcelに転記する簡単なコードです。

作業イメージ

openpyxl.JPG

ライブラリのインポート

from openpyxl import load_workbook
from openpyxl.cell.cell import Cell

エクセルの読込

#エクセルのファイルパスを指定
filepath1 = '店舗売上リスト.xlsx'
filepath2 = 'template.xlsx'

#エクセルを読み込む
wb1 = load_workbook(filename=filepath1)
ws1 = wb1['売上リスト']

wb2 = load_workbook(filename=filepath2)
ws2 = wb1['店舗情報']

セルの取得

for i in range(2, maxRow): #1行目は項目欄なので2行目から取得
    Cell_01 = ws1.cell(i,1)
    Cell_02 = ws1.cell(i,2)
    Cell_03 = ws1.cell(i,3)
    Cell_04 = ws1.cell(i,4)
    Cell_05 = ws1.cell(i,5)

    Cell_01_fill = str(Cell_01.value).zfill(4) #桁を揃える

    ws2[b1] = Cell_01_fill
    ws2[c5] = Cell_02.value
    ws2[c6] = Cell_03.value
    ws2[f3] = Cell_04.value
    ws2[f4] = Cell_04.value

ファイルの保存

filename = Cell_01_fill + '_' + str(Cell_02.value) + '_v01.xlsx'
wb2.save(filename)

参照元

PythonでExcelデータを転記して新しいファイルに保存|単純作業を自動化
https://fastclassinfo.com/entry/python_excel_filetenki/
OpenPyXl公式(英語)
https://openpyxl.readthedocs.io/en/stable/
OpenPyXlのチートシート
https://qiita.com/Maruchin/items/8713e39dbcbeaaa86023

参考

YouTubeで「Pythonを使った事務処理の効率化」というタイトルで、OpenPyXlを紹介。

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