Python openpyxl中的水平文本对齐
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26671581/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Horizontal text alignment in openpyxl
提问by Pythonizer
I'm tryign to change the text alignment to the center of 2 merged sells, I've found some answers that didn't work for my case
我正在尝试将文本对齐方式更改为 2 个合并销售的中心,我发现了一些对我的案例不起作用的答案
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'
both didn't work, is there any other ways to do it?
两者都不起作用,有没有其他方法可以做到?
采纳答案by samsemilia7
yes, there is a way to do this with openpyxl:
是的,有一种方法可以用 openpyxl 做到这一点:
from openpyxl.styles import Alignment
currentCell = ws.cell('A1') #or currentCell = ws['A1']
currentCell.alignment = Alignment(horizontal='center')
hope this will help you
希望能帮到你
回答by Tanveer Alam
You can achieve this by using Python XlsxWriterlibrary.
您可以使用 Python XlsxWriter库来实现这一点。
import xlsxwriter
workbook = xlsxwriter.Workbook('example.xlsx')
worksheet = workbook.add_worksheet()
cell_format = workbook.add_format({'align': 'center'})
worksheet.merge_range('A1:B1', "")
worksheet.write_rich_string('A1','Example', cell_format)
workbook.close()
Here i have merged cells A1, B1 and added a cell format parameter which includes the align parameter assigned as center.
在这里,我合并了单元格 A1、B1 并添加了一个单元格格式参数,其中包括分配为中心的对齐参数。


回答by nmz787
This is what finally worked for me with the latest version from PIP (2.2.5)
这就是 PIP (2.2.5) 的最新版本最终对我有用的方法
# center all cells
for col in w_sheet.columns:
for cell in col:
# openpyxl styles aren't mutable,
# so you have to create a copy of the style, modify the copy, then set it back
alignment_obj = cell.alignment.copy(horizontal='center', vertical='center')
cell.alignment = alignment_obj
回答by mhorne
None of the other solutions worked for me, since my solution requires openpyxl, and at least in 2.1.5 cell.alignment can't be set directly.
其他解决方案都不适合我,因为我的解决方案需要 openpyxl,并且至少在 2.1.5 cell.alignment 中不能直接设置。
from openpyxl.styles import Style, Alignment
cell = ws.cell('A1')
cell.style = cell.style.copy(alignment=Alignment(horizontal='center'))
The above copies the current style and replaces the alignment. You can also create a whole new style - with any values not specified taking the default values from https://openpyxl.readthedocs.org/en/latest/styles.html
以上复制当前样式并替换对齐方式。您还可以创建全新的样式 - 任何未指定的值均采用https://openpyxl.readthedocs.org/en/latest/styles.html 中的默认值
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

