Python 将边框应用于 OpenPyxl 中的单元格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24917201/
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
Applying borders to a cell in OpenPyxl
提问by user2961794
I am trying to use Openpyxl to apply a border to a cell, but I have failed on the most basic "apply any kind of border to any cell anywhere" task. I tried copying from the Openpyxl documentation (http://pythonhosted.org/openpyxl/styles.html#introduction) default style and modifying, but that gives me
我正在尝试使用 Openpyxl 将边框应用于单元格,但我在最基本的“将任何类型的边框应用于任何位置的任何单元格”任务中都失败了。我尝试从 Openpyxl 文档(http://pythonhosted.org/openpyxl/styles.html#introduction)中复制默认样式并进行修改,但这给了我
"TypeError:init() got an unexpected keyword argument 'superscript'"
“TypeError: init() 得到了一个意外的关键字参数‘上标’”
I tried copying straight out of another example here (Apply borders to all cells in a range with openpyxl), but that gives me
我尝试直接从这里的另一个示例中复制(使用 openpyxl 将边框应用于范围内的所有单元格),但这给了我
AttributeError: type object 'Border' has no attribute 'BORDER_THIN'
AttributeError: 类型对象 'Border' 没有属性 'BORDER_THIN'
(even after I fix the typos and insufficient imports errors).
(即使在我修复了拼写错误和导入不足错误之后)。
Does anyone know how to apply borders using Python 3.3 and OpenPyxl 2.0.4? All I'm looking for is a snippet of code that, if I copy-paste it into a blank script, will put a border around any cell in a workbook.
有谁知道如何使用 Python 3.3 和 OpenPyxl 2.0.4 应用边框?我正在寻找的只是一段代码,如果我将其复制粘贴到空白脚本中,则会在工作簿中的任何单元格周围放置一个边框。
回答by FriendFX
With openpyxl version 2.0.4, this snippet works for me:
使用 openpyxl 2.0.4 版,此代码段对我有用:
from openpyxl.styles.borders import Border, Side
from openpyxl.styles import Style
from openpyxl import Workbook
thin_border = Border(left=Side(style='thin'),
right=Side(style='thin'),
top=Side(style='thin'),
bottom=Side(style='thin'))
my_style = Style(border=thin_border)
wb = Workbook()
ws = wb.get_active_sheet()
ws.cell(row=3, column=2).style = my_style
wb.save('border_test.xlsx')
回答by takasu
With openpyxl version 2.2.5, this snippet works for me:
使用 openpyxl 2.2.5 版,此代码段对我有用:
from openpyxl.styles.borders import Border, Side
from openpyxl import Workbook
thin_border = Border(left=Side(style='thin'),
right=Side(style='thin'),
top=Side(style='thin'),
bottom=Side(style='thin'))
wb = Workbook()
ws = wb.get_active_sheet()
# property cell.border should be used instead of cell.style.border
ws.cell(row=3, column=2).border = thin_border
wb.save('border_test.xlsx')
The documentation mentions other values for the style attribute:
Value must be one of {‘double', ‘dashed', ‘thin', ‘medium', ‘mediumDashDot', ‘dashDot', ‘thick', ‘mediumDashed', ‘hair', ‘dotted', ‘slantDashDot', ‘mediumDashDotDot', ‘dashDotDot'}
值必须是 {'double', 'dashed', 'thin', 'medium', 'mediumDashDot', 'dashDot', 'thick', 'mediumDashed', 'hair', 'dotted', 'slantDashDot', 'mediumDashDotDot', 'dashDotDot'}
回答by makkasi
This answer works with version 2.4.8The difference with the previous two answers is that the property for Side is border_style, not style
此答案适用于版本2.4.8与前两个答案的不同之处在于 Side 的属性是border_style,而不是 style
from openpyxl.styles.borders import Border, Side, BORDER_THIN
thin_border = Border(
left=Side(border_style=BORDER_THIN, color='00000000'),
right=Side(border_style=BORDER_THIN, color='00000000'),
top=Side(border_style=BORDER_THIN, color='00000000'),
bottom=Side(border_style=BORDER_THIN, color='00000000')
)
ws.cell(row=3, column=2).border = thin_border
Working with styles: https://openpyxl.readthedocs.io/en/2.5/styles.html