擅长:python、mysql、java
<p>其他的解决方案都不适合我,因为我的解决方案需要openpyxl,而且至少在2.1.5 cell.alignment中不能直接设置。</p>
<pre><code>from openpyxl.styles import Style, Alignment
cell = ws.cell('A1')
cell.style = cell.style.copy(alignment=Alignment(horizontal='center'))
</code></pre>
<p>以上内容将复制当前样式并替换对齐方式。
您还可以创建一个全新的样式-任何未指定的值都取<a href="https://openpyxl.readthedocs.org/en/latest/styles.html" rel="nofollow">https://openpyxl.readthedocs.org/en/latest/styles.html</a>中的默认值</p>
<pre><code>cell.style = Style(alignment=Alignment(horizontal='center'),font=Font(bold=True))
# or - a tidier way
vals = {'alignment':Alignment(horizontal='center'),
'font':Font(bold=True),
}
new_style = Style(**vals)
cell.style = new_style
</code></pre>