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)
            cwb = Workbook() 
        #Sorce workbook's sheet    

        #Verify if destination workbbok's sheet exists or create new one
        if destSheet in cwb.sheetnames:

        #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)
                    if column.has_style:
                        #Style copying give Unknow Style exception
                        #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)
               = copy(
                        cell.alignment = copy(column.alignment)
                        cell._style = copy(column._style)

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

            #Saving to destination
        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