pythonxlsxminidom

Cannot save modifications made in xlsx file


I read a .xlsx file, update it but Im not able to save it

from xml.dom import minidom as md
        [... some code ....]
        sheet = workDir + '/xl/worksheets/sheet'
        sheet1 = sheet + '1.xml'
        importSheet1 = open(sheet1,'r')
        whole_file= importSheet1.read()
        data_Sheet = md.parseString(whole_file)
        [... some code ....]
            self.array_mem_name = []
            y = 1
            x = 5 #first useful row
            day = int(day)
            found = 0
            while x <= len_array_shared:
                readrow = data_Sheet.getElementsByTagName('row')[x]
                c_data = readrow.getElementsByTagName('c')[0]
                c_attrib = c_data.getAttribute('t')
                if c_attrib == 's':
                    vName = c_data.getElementsByTagName('v')[0].firstChild.nodeValue
                    #if int(vName) != broken:
                    mem_name = self.array_shared[int(vName)]
                    if mem_name != '-----':
                        if mem_name == old:
                            c_data = readrow.getElementsByTagName('c')[day]
                            c_attrib = c_data.getAttribute('t')
                            if (c_attrib == 's'):
                                v_Attrib = c_data.getElementsByTagName('v')[0].firstChild.nodeValue
                                if v_Attrib != '':
                                    #loc = self.array_shared[int(v_Attrib)]
                                    index = self.array_shared.index('--')
                                    c_data.getElementsByTagName('v')[0].firstChild.nodeValue = index
    
                                    with open(sheet1, 'w') as f:
                                        f.write(whole_file)

As you can see I use f.write(whole_file) but whole_file has not the changes made with index. Checking the debug I see that the new value has been added to the node, but I can't save sheet1 with the modified value


Solution

  • I switched to using openpyxl instead, as was suggested in a comment by Lei Yang. I found that this tool worked better for my jobs. With openpyxl, reading cell values is much easier than with xml.dom.minidom.

    My only concern is that openpyxl seems really slower than the dom to load the workbook. Maybe the memory was overloaded. But, I was more interested in using something simpler than this minor performance issue.