简体   繁体   English

Openpyxl 边界线在与另一条边界线相交处中断

[英]Openpyxl Border line breaks on intersection with another border line

I'm trying to create a table to tabulate data with python's third party excel package, openpyxl.我正在尝试创建一个表,用 python 的第三方 excel 包 openpyxl 来制表数据。 The first for statement of the code draws a row line from cell 'A5' to cell 'E5', The second for statement draws another row line from cell 'A6' to cell 'E6' The third for statement draws a column line from cell 'C5' to cell 'C9' The fourth for statement draws another column from cell 'F5 to cell 'F9'代码的第一个 for 语句从单元格 'A5' 到单元格 'E5' 绘制行线,第二个 for 语句从单元格 'A6' 到单元格 'E6' 绘制另一行线 第三个 for 语句从单元格绘制列线'C5' 到单元格 'C9' 第四个 for 语句从单元格 'F5' 到单元格 'F9' 绘制另一列

However, the column border drawn from cell 'C5' to 'C9' seems to break just when it intersects the row lines from cell 'A5 to 'E5' and 'A6' to 'E6',and starts drawing at 'C7' instead of 'C5'.但是,从单元格“C5”到“C9”绘制的列边框似乎在与从单元格“A5”到“E5”和“A6”到“E6”的行线相交时中断,并在“C7”处开始绘制'C5'。

While the column border line drawn from cell 'F5':'F9' worked perfectly fine.虽然从单元格 'F5':'F9' 绘制的列边界线工作得很好。 (I would have posted an image of the table being created but i have a less-than-10 reputation) Is there anything i'm doing wrong somewhere? (我会发布正在创建的表格的图像,但我的声誉不到 10)我在某处做错了什么吗?

from openpyxl import Workbook
from openpyxl.styles import Border, Side
wb = Workbook()
ws = wb.active
ws.sheet_view.showGridLines = False

column_border = Border(left=Side(style='thin'))
row_border = Border(top=Side(style='thin'))

for col in ws['C5:C9']:
    for cell in col:
        cell.border = column_border
for col in ws['F5:F9']:
    for cell in col:
        cell.border = column_border

for row in ws['A5:E5']:
    for cell in row:
        cell.border = row_border
for col in ws['A6:E6']:
    for cell in col:
        cell.border = row_border

wb.save('a.xlsx')

The issue is that you are over-writing the Border object of those cells where they intersect.问题是您正在覆盖它们相交的那些单元格的 Border 对象。 Your first two loops set the Border object of cells to be a Thin Line on the Left side.您的前两个循环将单元格的 Border 对象设置为左侧的细线。 The next two loops set the Border Object of the cells to be aa Thin Line on the Top - but this gets rid of the original Border object that created the top line.接下来的两个循环将单元格的边框对象设置为顶部的细线 - 但这摆脱了创建顶部线条的原始边框对象。

What I would recommend doing is create two methods add the border object.我建议做的是创建两个方法来添加边框对象。 The methods would check if the border already exists.这些方法将检查边界是否已经存在。 If it does, it would augment it rather than replace it.如果是这样,它将扩大它而不是取代它。 Then you call these methods in your loops rather than do the direct assignment.然后在循环中调用这些方法而不是直接赋值。

This code may or may not work, my current system doesn't have OpenPyXL installed and I need admin privileges to install it.此代码可能有效,也可能无效,我当前的系统没有安装 OpenPyXL,我需要管理员权限才能安装它。

def add_row_boarder(row_cell):
    if row_cell.border = None:
        row_cell.border = row_border
    else:
        row_cell.border.top = Side(style='thin')
    return row_cell

def add_col_boarder(col_cell):
    if col_cell.border = None:
        col_cell.border = col_border
    else:
        col_cell.border.left = Side(style='thin')
    return col_cel

As Rob notes, you're overwriting the borders of some cells.正如 Rob 所指出的,您正在覆盖某些单元格的边界。 A generic approach to formatting a range of cells is provided by the documentation but basically you can just combine styles so cell.border = cell.border + row_border 文档提供了格式化一系列单元格的通用方法,但基本上您可以组合样式,因此cell.border = cell.border + row_border

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM