dataDxfId on tablecolumns lead to repair xml

Issue #1231 invalid
Manoj Choudhury
created an issue

Hi On cloning one one xlsx file to another ,the tablecolumn instance gets datafxid=0 value.When opening with Excel it is repaired by removing it. I have fixed by setting it none for all tablecolumn instances.

import re
from openpyxl import load_workbook,Workbook
from pathlib import Path
from copy import copy, deepcopy
from openpyxl.worksheet.table import Table, TableStyleInfo
from openpyxl.xml.functions import tostring


def copyexcel(src,srcsheet,dest,destSheet):
        #Load source workbook
        wb = load_workbook(src)

        #Verify destination workbook exists or else create new one
        tocopy_file = Path(dest)
        if tocopy_file.is_file():
            cwb = load_workbook(dest)
        else:
            cwb = Workbook() 
        #Sorce workbook's sheet    
        sheet=wb[srcsheet] 

        #Verify if destination workbbok's sheet exists or create new one
        if destSheet in cwb.sheetnames:
            csheet=cwb[destSheet]
        else:
            csheet=cwb.active
            csheet.title=destSheet


        #iterate of Source sheet cells and copy to destination sheet
        for (i,row) in enumerate(sheet):
            for (j,column) in enumerate(row):
                    cell=csheet.cell(row = i+1, column = j+1)
                    cell.value=column.value
                    #print(column.font)
                    if column.has_style:
                        #Style copying give Unknow Style exception
                        #cell.style = column.style
                        #Copying each style properties
                        cell.font = copy(column.font)
                        cell.border = copy(column.border)
                        cell.fill = copy(column.fill)
                        cell.number_format = copy(column.number_format)
                        cell.protection = copy(column.protection)
                        cell.alignment = copy(column.alignment)
                        cell._style = copy(column._style)

        csheet._tables=copy(sheet._tables)
        #fix for dataDxfId=0 bug
        for (i,table) in enumerate(sheet._tables):
            for (j,tableColumn) in enumerate(table.tableColumns):
                tableColumn.dataDxfId=None

        try:
            #Saving to destination
            cwb.save(dest)
        except (IOError, OSError) as e:
            print('Unable to write to file')

        # print sheet names
        # print(wb.sheetnames,cwb.sheetnames)


copyexcel("SyncSettings - Copy.xlsx","Sheet1","SyncSettingsUpdated34.xlsx","Sheet1")

Comments (5)

  1. Log in to comment