【问题标题】:Apply borders in many cells openpyxl在许多单元格中应用边框openpyxl
【发布时间】:2017-03-07 19:56:47
【问题描述】:

我是新手,我想知道你是否可以帮助我我不明白如何使用openpyxl选择多个单元格并应用边框,如果你能举个例子。谢谢

【问题讨论】:

    标签: border apply openpyxl


    【解决方案1】:

    现在您可以直接在工作表中进行迭代。我修改了代码

    def set_border(ws, cell_range):
        rows = list(ws[cell_range])
        side = Side(border_style='thin', color="FF000000")
    
        rows = list(rows)  # we convert iterator to list for simplicity, but it's not memory efficient solution
        max_y = len(rows) - 1  # index of the last row
        for pos_y, cells in enumerate(rows):
            max_x = len(cells) - 1  # index of the last cell
            for pos_x, cell in enumerate(cells):
                border = Border(
                    left=cell.border.left,
                    right=cell.border.right,
                    top=cell.border.top,
                    bottom=cell.border.bottom
                )
                if pos_x == 0:
                    border.left = side
                if pos_x == max_x:
                   border.right = side
                if pos_y == 0:
                   border.top = side
                if pos_y == max_y:
                   border.bottom = side
    
                # set new border only if it's one of the edge cells
                if pos_x == 0 or pos_x == max_x or pos_y == 0 or pos_y == max_y:
                   cell.border = border
    

    然后这样称呼它:

    set_border(ws, 'A16:I17')
    

    【讨论】:

      【解决方案2】:

      属性称为边框(不是边框)

      试试这个:

           def set_border(ws, cell_range):
          rows = list(ws.iter_rows(cell_range))
          side = Side(border_style='thin', color="FF000000")
      
          rows = list(rows)  # we convert iterator to list for simplicity, but it's not memory efficient solution
          max_y = len(rows) - 1  # index of the last row
          for pos_y, cells in enumerate(rows):
              max_x = len(cells) - 1  # index of the last cell
              for pos_x, cell in enumerate(cells):
                  border = Border(
                      left=cell.border.left,
                      right=cell.border.right,
                      top=cell.border.top,
                      bottom=cell.border.bottom
                  )
                  if pos_x == 0:
                      border.left = side
                  if pos_x == max_x:
                      border.right = side
                  if pos_y == 0:
                      border.top = side
                  if pos_y == max_y:
                      border.bottom = side
      
                  # set new border only if it's one of the edge cells
                  if pos_x == 0 or pos_x == max_x or pos_y == 0 or pos_y == max_y:
                      cell.border = border
      

      【讨论】:

      • 代码编译没有错误,但我看不到边框
      猜你喜欢
      • 1970-01-01
      • 2016-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多