Skip to content

Instantly share code, notes, and snippets.

@oyakata
Created July 1, 2014 05:44
Show Gist options
  • Save oyakata/824dad8fa3eb4d729790 to your computer and use it in GitHub Desktop.
Save oyakata/824dad8fa3eb4d729790 to your computer and use it in GitHub Desktop.
openpyxlの使い方(背景色と罫線をつける)
# -*- coding:utf-8 -*-
import openpyxl as ox
# インポートはもっときれいな方法があるかも
from openpyxl.styles.fills import FILL_SOLID
from openpyxl.styles.colors import Color, RED, BLUE
from openpyxl.styles.fills import PatternFill
from openpyxl.styles.borders import Border, Side as BorderSide, BORDER_THIN
# ソースコードはBitbucketにあるので注意。Githubではない。
# openpyxl==2.0.4
# https://bitbucket.org/ericgazoni/openpyxl/src/f6e73078b66d/openpyxl/?at=default
book = ox.Workbook()
sheet = book.active
sheet.title = u"お-と-し-ま-え"
sheet.cell("A1").value = u"滝川"
cell = sheet.range("C8")
cell.value = u"ここが色と罫線がつくよ!!!"
cell.style = cell.style.copy(
fill=PatternFill(
start_color=Color(RED),
end_color=Color(RED),
fill_type=FILL_SOLID,
),
border=cell.style.border.copy(
left=BorderSide(color=Color(BLUE), style=BORDER_THIN),
right=BorderSide(color=Color(BLUE), style=BORDER_THIN),
top=BorderSide(color=Color(BLUE), style=BORDER_THIN),
bottom=BorderSide(color=Color(BLUE), style=BORDER_THIN),
),
)
book.save(filename="hey.xlsx")
@JerryX
Copy link

JerryX commented May 31, 2016

it's not work,
TypeError: copy() got an unexpected keyword argument 'fill'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment