我想将两个合并单元格的文本对齐方式更改为居中。我找到了一些答案,但对我的情况不起作用:
currentCell = ws.cell('A1')
currentCell.style.alignment.horizontal = 'center' #TypeError: cannot set horizontal attribute
#or
currentCell.style.alignment.vertical = Alignment.HORIZONTAL_CENTER #AttributeError: type object 'Alignment' has no attribute 'HORIZONTAL_CENTER'
两种方法都不起作用,还有其他方法吗?
Style
类已不再使用,您可以直接为各个样式元素赋值。例如,在此示例中,您将分别指定cell.font = Font(bold=True)
和cell.alignment = Alignment(horizontal='center')
(而不是在示例中)以及cell.fill = PatternFill(fgColor='33489F', fill_type='solid')
。 - hlongmore