Issue copying the borders

Issue #912 resolved
Uchiha Madara
created an issue

copying borders and and making changes in the copied borders reflects changes in the parent border too. However font, fill, alignment are working as expected.

from openpyxl.styles import PatternFill, Border, Side, Alignment, Protection, Font,NamedStyle


border1 = Border(left=Side(border_style=None,color='FF000000'),
                     right=Side(border_style=None,color='FF000000'),
                     top=Side(border_style=None,color='FF000000'),
                     bottom=Side(border_style=None,color='FF000000'),
                     diagonal=Side(border_style=None,color='FF000000'),
                     diagonal_direction=0,
                     outline=Side(border_style=None,color='FF000000'),
                     vertical=Side(border_style=None,color='FF000000'),
                     horizontal=Side(border_style=None,color='FF000000'))

border2 = copy(border1)
border2.bottom.border_style='hair'

print(border1.bottom.border_style)  # supposed to print None but prints 'hair'
print(border2.bottom.border_style)

Comments (6)

  1. Andrew Thornton

    I've just submitted a fix for the test bug caused the changeset - however, I've noticed a slight issue:

            new_style = copy.copy(wb._named_styles[0])
    

    will now cause a copy of the wb to be generated within the new named_style. I guess that's not really what's intended - but it's strictly what a deepcopy is.

  2. CharlieC

    Thanks for the PR. For some reason I didn't get the test failure when I made the change. Creating a copy of the workbook is definitely undesirable. Might be best to revert the change to deepcopy and leave it to client code to decide. Copying in Python of nested structures is always tricky. But it might be sufficient to use deepcopyonly for those style objects like borders where nesting is expected.

  3. Log in to comment