如何在openpyxl中进行列对齐?

问题描述 投票:0回答:1

我试图在openpyxl中对齐一列,但没有成功:

wb = Workbook()
ws = wb.active

....

ws.column_dimensions["A"].alignment = Alignment(horizontal='left')
ws.column_dimensions["C"].alignment = Alignment(horizontal='center')

相同:

ws["A"].alignment = Alignment(horizontal='left')
ws["C"].alignment = Alignment(horizontal='center')

这对我不起作用。否则,遍历单元格并进行对齐将正常工作!

python excel openpyxl
1个回答
4
投票

您应该获得失败的回溯!如果您发布问题,请向我们显示!

ws["A"].alignment = Alignment(horizontal='left')
 AttributeError: 'tuple' object has no attribute 'alignment'

甚至整个列]的样式未实现。您必须自己做。例如:for row in rows: cell_A = row[:1][0] cell_A.alignment = Alignment(horizontal='left')

经过Python:3.4.2测试-openpyxl:2.4.1-LibreOffice:4.3.3.2

© www.soinside.com 2019 - 2024. All rights reserved.