sttsのソースコードMemoブログ

色々やってみた結果のMemo

python excelファイル(xlsx)を操作するopenpyxlの使い方メモ

インストールは下記

$ pip install --user openpyxl

下記で新しいexcelファイルを作る

from openpyxl import Workbook
wb = Workbook()
ws = wb.active
ws["A1"] = "hoge" wb.save("test.xlsx")

B2セルに枠線を付け、背景色を付け、C列と3行目の幅を変える例は下記

from openpyxl import Workbook
from openpyxl.styles import PatternFill, Border, Side, Alignment, Protection, Font

wb = Workbook()
ws = wb.active

ws["B2"] = "foo"

style=Side(border_style="thin", color="000000")
border = Border(top=style, left=style, right=style, bottom=style)
ws["B2"].border=border

fill=PatternFill("solid", fgColor="DDDDDD")
ws["B2"].fill=fill

ws.column_dimensions["C"].width = 20
ws.row_dimensions[3].height = 40

wb.save("test.xlsx")

調べていると作ろうとしたのものがもうあった。画像ファイルを読み込んでexcelファイルにするスクリプト

http://labs.timedia.co.jp/2017/07/pythonopenpypxexcel.html