Openpyxl - create styles as variables
<p>I want apply formats to excel cells (change default fonts and cells fills). I can set the cell attributes one by one:</p>

<pre><code>from openpyxl.styles import PatternFill, Border, Side, Alignment, Protection, Font, Color

ws1.cell(row=1,column=1).font=Font(color=colors.WHITE,size=9)
ws1.cell(row=1,column=1).fill=PatternFill(fill_type="solid", start_color='0000FF', end_color='0000FF')
...and so on
</code></pre>

<p>Is there a more efficient way of doing this, by creating template styles, so I can only write something like</p>

<pre><code>ws1.cell(row=1,column=1).style=TemplateStyle
</code></pre>

<p>Maybe 'named styles' are an option. I think this is what @Charlie Clark was talking about. You can find a rather limited explanation on the <a href="http://openpyxl.readthedocs.io/en/default/styles.html#creating-a-named-style" rel="nofollow">openpyxl 2.4.0 documentation</a></p>

<p>This could look something like the following. </p>

<pre><code>from openpyxl.styles import NamedStyle, Font, PatternFill

template_style = NamedStyle(name="template_style")
template_style.font = Font(color=colors.WHITE,size=9)
template_style.fill = PatternFill(fill_type="solid", start_color='0000FF', end_color='0000FF')

ws1.cell(row=1, column=1).style = template_style
</code></pre>

<p>I am not sure how to reference to the name inside the brackets of NamedStyle, but I do know that it does not need to be the same as the variable to which it is associated to. Since the documentation uses both the same name for the variable and name in brackets, I choose to do the same.</p>

python styles openpyxl