| 次のリビジョン | 前のリビジョン |
| python:openpyxl [2019/03/04 11:11] – 作成 mumeiyamibito | python:openpyxl [2019/03/04 14:51] (現在) – mumeiyamibito |
|---|
| <code python>import openpyxl</code> | <code python>import openpyxl</code> |
| |
| ==== ワークブックオブジェクトの作成 ==== | ==== ワークブックの操作 ==== |
| * 新規作成<code python>wb = openpypxl.Workbook()</code> | * 新規作成<code python>wb = openpypxl.Workbook()</code> |
| * 既存の xlsx ファイルの読み込み<code python>wb = openpyxl.load_workbook("INPUT.xlsx") | * 既存の xlsx ファイルの読み込み<code python>wb = openpyxl.load_workbook(INPUT)</code> |
| * ''INPUT.xls'': 既存の xlsx ファイルのパス | * ''INPUT.xls'': 読み込む xlsx ファイルのパス |
| | * 保存<code python>wb.save(OUTPUT)</code> |
| | * ''OUTPUT'': 保存先のパス |
| |
| ==== ワークシートの操作 ==== | ==== ワークシートの操作 ==== |
| * シートの新規作成<code python>ws = wb.create_sheet("SHEET_NAME", POS) | * シートの新規作成<code python>ws = wb.create_sheet("SHEET_NAME", POS)</code> |
| * ''ws'': ワークシートオブジェクト (ワークシートを操作する場合は、この変数に対して行う) | * ''ws'': ワークシートオブジェクト (ワークシートを操作する場合は、この変数に対して行う) |
| * ''SHEET_NAME'': シート名 | * ''SHEET_NAME'': シート名 |
| * ''ws'': ワークシートオブジェクト | * ''ws'': ワークシートオブジェクト |
| * ''wb'': ワークブックオブジェクト | * ''wb'': ワークブックオブジェクト |
| * ワークシート名で選択<code python>wb["SHEET_NAME"] | * ワークシート名で選択<code python>wb["SHEET_NAME"]</code> |
| * ワークシート名の変更<code python>ws.title = "NAME"</code> | * ワークシート名の変更<code python>ws.title = "NAME"</code> |
| * ''ws'': ワークシートオブジェクト | * ''ws'': ワークシートオブジェクト |
| * ワークシートの削除<code python>wb.remove_sheet(ws) | * ワークシートの削除<code python>wb.remove_sheet(ws)</code> |
| * ''wb'': ワークブックオブジェクト | * ''wb'': ワークブックオブジェクト |
| * ''ws'': ワークシートオブジェクト | * ''ws'': ワークシートオブジェクト |
| * オブジェクト一覧<code python>wb.worksheets</code> | * オブジェクト一覧<code python>wb.worksheets</code> |
| * シート名一覧<code python>wb.sheetnames</code> | * シート名一覧<code python>wb.sheetnames</code> |
| | |
| | ==== セルの操作 ==== |
| | * セルの値の変更 |
| | * <code python>ws.cell(row = I, column = J, value = V)</code> |
| | * ''ws'': ワークシートオブジェクト |
| | * ''I'': セルの行番号 (1 から始まる) |
| | * ''J'': セルの列番号 (1 から始まる) |
| | * ''V'': 代入する値 |
| | * <code python>ws.cell(row = I, column = J).value = V</code> |
| | * ''ws'': ワークシートオブジェクト |
| | * ''I'': セルの行番号 (1 から始まる) |
| | * ''J'': セルの列番号 (1 から始まる) |
| | * ''V'': 代入する値 |
| | * <code python>ws["POS"] = V</code> |
| | * ''ws'': ワークシートオブジェクト |
| | * ''POS'': エクセルのセルの指定表記 (A1 や B2 など) |
| | * ''V'': 代入する値 |
| | * 1 つのセルの読み込み |
| | * <code python>ws.cell(row = I, column = J).value</code> |
| | * ''ws'': ワークシートオブジェクト |
| | * ''I'': セルの行番号 (1 から始まる) |
| | * ''J'': セルの列番号 (1 から始まる) |
| | * <code python>ws["POS"].value</code> |
| | * ''ws'': ワークシートオブジェクト |
| | * ''POS'': エクセルのセルの指定表記 (A1 や B2 など) |
| | * 複数のセルの読み込み |
| | * <code python>ws.iter_rows(min_row = MinR, max_row = MaxR, min_col = MinC, max_col = MaxC)</code> |
| | * ''ws'': ワークシートオブジェクト |
| | * ''MinR'': 最小の行番号 (1 から始まる) |
| | * ''MaxR'': 最大の行番号 (1 から始まる) |
| | * ''MinC'': 最小の列番号 (1 から始まる) |
| | * ''MaxC'': 最大の列番号 (1 から始まる) |
| | * タプルに値が格納されたジェネレータが返される。 |
| | * <code python>ws.values</code> |
| | * ''ws'': ワークシートオブジェクト |
| | * すべてのセルオブジェクトがジェネレータが返される。 |
| | * <code python>ws["POS:POS"]</code> |
| | * ''ws'': ワークシートオブジェクト |
| | * ''POS:POS'': Excel の複数セルの表記 (A1:B2 -> 2x2) |
| | * セルオブジェクトが格納された二次元タプルが返される。 |
| | * <code python>ws.rows</code> |
| | * ''ws'': ワークシートオブジェクト |
| | * セルオブジェクトが格納されたタプルが行単位のジェネレータで返される。 |
| | * <code python>ws.columns</code> |
| | * ''ws'': ワークシートオブジェクト |
| | * セルオブジェクトが格納されたタプルが列単位のジェネレータで返される。 |
| | * 例: すべてのセルを for で読み込む<code python> |
| | for row in ws: |
| | for cell in row: |
| | print(cell.value) |
| | </code> |
| | * ''ws'': ワークシートオブジェクト |
| | * セルの表示形式を変更する (''cell'' はセルオブジェクト)。 |
| | * 標準<code python>cell.number_format = openpyxl.styles.numbers.FORMAT_GENERAL</code> |
| | * 何も指定しない場合は、自動的にこのフォーマットになる。 |
| | * 数値 |
| | * 数値のデフォルト<code python>cell.number_format = openpyxl.styles.numbers.FORMAT_NUMBER</code> |
| | * 桁数を指定<code python>cell.number_format = "0.0_"</code> |
| | * ''0_''、''0.0_''、''0.00_'' のように桁数を指定する。 |
| | * コンマ区切り<code python>cell.number_format = "#,##0.0_"</code> |
| | * 桁数指定の前に ''#,##'' を付ける。 |
| | * パーセント表示<code python>cell.number_format = "0.0%"</code> |
| | * 数値の表記の後に ''%'' を付ける。 |
| |
| |
| ===== 参考サイト ===== | ===== 参考サイト ===== |
| * [[https://note.nkmk.me/python-openpyxl-usage/ | PythonでExcelファイル(xlsx)を読み書きするopenpyxlの使い方 | note.nkmk.me]] | * [[https://note.nkmk.me/python-openpyxl-usage/ | PythonでExcelファイル(xlsx)を読み書きするopenpyxlの使い方 | note.nkmk.me]] |
| | * [[http://note.crohaco.net/2017/python-openpyxl-excel/ | [Python] openpyxl で Excel を操作してみた! - くろのて]] |
| | * [[https://pg-chain.com/python-excel-format | Python Excelのセルにフォーマットを指定する | 鎖プログラム]] |
| |
| |
| {{tag>プログラミング}} | {{tag>プログラミング}} |
| |