python – 在OpenPyxl中将边框应用于单元格

我正在尝试使用Openpyxl将边框应用于单元格,但我在最基本的“将任何类型的边框应用于任何位置的任何单元格”任务上都失败了.我尝试从Openpyxl文档(http://pythonhosted.org/openpyxl/styles.html#introduction)默认样式进行复制并修改,但这给了我

“TypeError:init() got an unexpected keyword argument ‘superscript'”

我试着在这里直接复制另一个例子(Apply borders to all cells in a range with openpyxl),但这给了我

AttributeError: type object ‘Border’ has no attribute ‘BORDER_THIN’

(即使我修正了拼写错误和导入错误不足).

有谁知道如何使用Python 3.3和OpenPyxl 2.0.4应用边框?我正在寻找的是一段代码,如果我将其复制粘贴到空白脚本中,将在工作簿中的任何单元格周围放置边框.

解决方法:

使用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')
上一篇:从工作簿复制列,粘贴第二个工作簿的第二张,openPyXL


下一篇:python – 在一些单元格openpyxl中添加超链接