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 1 year has passed since last update.

【一分理解】pythonでexcelを操作しよう

Last updated at Posted at 2022-03-24

pythonでexcel操作をしよう!

こんな時に使える

・大量のExcelを管理するとき
・excelに保存したデータでpython動かしたい時

流れ

excelファイルを読み込む

file = openpyxl.load_workbook("ファイルのパス")

中身をいじる
●シート操作

    sheet = file['Sheet1']

    file.create_sheet()  # シートを新規追加
    file.create_sheet(index=0, title='new_sheet')  # 指定の場所にシートを新規追加
    file.create_sheet(index=2, title='new2_sheet')  # 指定の場所(2番目)に名前を付けてシートを追加
    del file['new_sheet']

●セル操作

    # A1セルを取得
    cell_a1 = sheet['A1'].value

    # 各セルに値(数式)をセット
    sheet['A2'] = 300
    sheet['A3'] = '=SUM(A1:A2)'  # 数式を適用

    # 列の挿入
    sheet.insert_rows(7)

    # 最終列に値を追加
    sheet.append(["こんにちは", "こんばんは"])

●保存を忘れずに

    file.save("保存先のパス")

サンプルソース
MyGitHub

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?