💡
PythonでExcel操作、OpenPyXLチートシート
$ pip install openpyxl
import openpyxl
wb = openpyxl.load_workbook("sample.xlsx")
# マクロ有のExcelファイルの場合は以下のオプションをつける
wb = openpyxl.load_workbook("sample.xlsm" , keep_vba=True)
wb.save("sample.xlsx")
wb.sheetnames
# => ["sheet_A", "sheet_B", "sheet_C"]
ws_copy = wb.copy_worksheet(wb["sheet_A"])
ws_copy.title = "sheet_D"
wb["sheet_name"]["A1"].value
wb["sheet_name"]['A1'] = "NEW CELL VALUE"
wb["sheet_name"]['A1'] = "=SUM(B1:C1)"
wb["sheet_name"].merge_cells("A1:D3")
chart = openpyxl.chart.BarChart()
chart.type = "bar"
chart.title = "Bar Chart"
# データの登録
src = openpyxl.chart.Reference(ws, min_col=1, min_row=1, max_col=5, max_row=4)
chart.add_data(src, titles_from_data=True)
# 項目名の設定
cat = openpyxl.chart.Reference(ws_copy, min_col=2, max_col=5, min_row=1)
chart.set_categories(cat)
chart.anchor = 'A8'
ws.add_chart(chart)
©︎ 2024 - Yard