Monday, 23 August 2021

Change fileshare root in Excel dynamically by modifying the xml behind the Excel with Python

We are at a point of a fileshare migration but a lot of excel files on the current share reference each other including roots (in formulas). I figured it's possible to change the formula references of the current root to the new root by modifying the underlying xml.

I did modify one tag value of the XML, hoping that the structure of the XML would stay the same. However unfortunately the structure doesn't stay the same.

Tag: <x15ac:absPath xmlns:x15ac="http://schemas.microsoft.com/office/spreadsheetml/2010/11/ac" url="Q:\General\Results\2021\"

should be modified to <x15ac:absPath xmlns:x15ac="http://schemas.microsoft.com/office/spreadsheetml/2010/11/ac" url="I:\Results\2021\"

As you can see the structure and tags changes and I can't come up with the reason for that. Appreciate your help on solving this. Please let me know if I need to clarify.

My python code:

import xml.etree.ElementTree as ET

# Change workbook.xml
tree = ET.parse('workbook.xml')
root = tree.getroot()
for elem in root.iter():
    if str(elem.get('url'))[:11] == 'Q:\General':
        elem.set('url', elem.get('url').replace(r'Q:\General', 'I:\Results'))
tree.write('workbook.xml', encoding='UTF-8', method='xml',  xml_declaration=True)

XML of initial excel file (workbook.xml):

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<workbook
    xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"
    xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="x15 xr xr6 xr10 xr2"
    xmlns:x15="http://schemas.microsoft.com/office/spreadsheetml/2010/11/main"
    xmlns:xr="http://schemas.microsoft.com/office/spreadsheetml/2014/revision"
    xmlns:xr6="http://schemas.microsoft.com/office/spreadsheetml/2016/revision6"
    xmlns:xr10="http://schemas.microsoft.com/office/spreadsheetml/2016/revision10"
    xmlns:xr2="http://schemas.microsoft.com/office/spreadsheetml/2015/revision2">
    <fileVersion appName="xl" lastEdited="7" lowestEdited="7" rupBuild="23426"/>
    <workbookPr defaultThemeVersion="166925"/>
    <mc:AlternateContent
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006">
        <mc:Choice Requires="x15">
            <x15ac:absPath url="Q:\General\Results\2021\"
                xmlns:x15ac="http://schemas.microsoft.com/office/spreadsheetml/2010/11/ac"/>
            </mc:Choice>
        </mc:AlternateContent>
        <xr:revisionPtr revIDLastSave="0" documentId="13_ncr:1_{77E8B687-2130-4B59-8784-F53BD3BD2847}" xr6:coauthVersionLast="45" xr6:coauthVersionMax="45" xr10:uidLastSave="{00000000-0000-0000-0000-000000000000}"/>
        <bookViews>
            <workbookView xWindow="28680" yWindow="-120" windowWidth="29040" windowHeight="15990" xr2:uid="{00000000-000D-0000-FFFF-FFFF00000000}"/>
        </bookViews>
        <sheets>
            <sheet name="Blad1" sheetId="1" r:id="rId1"/>
        </sheets>
        <calcPr calcId="191029"/>
        <extLst>
            <ext uri="{140A7094-0E35-4892-8432-C4D2E57EDEB5}"
                xmlns:x15="http://schemas.microsoft.com/office/spreadsheetml/2010/11/main">
                <x15:workbookPr chartTrackingRefBase="1"/>
            </ext>
            <ext uri="{B58B0392-4F1F-4190-BB64-5DF3571DCE5F}"
                xmlns:xcalcf="http://schemas.microsoft.com/office/spreadsheetml/2018/calcfeatures">
                <xcalcf:calcFeatures>
                    <xcalcf:feature name="microsoft.com:RD"/>
                    <xcalcf:feature name="microsoft.com:Single"/>
                    <xcalcf:feature name="microsoft.com:FV"/>
                    <xcalcf:feature name="microsoft.com:CNMTM"/>
                    <xcalcf:feature name="microsoft.com:LET_WF"/>
                </xcalcf:calcFeatures>
            </ext>
        </extLst>
    </workbook>

Output XML after python code:

<?xml version='1.0' encoding='UTF-8'?>
<ns0:workbook
    xmlns:ns0="http://schemas.openxmlformats.org/spreadsheetml/2006/main"
    xmlns:ns1="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:ns2="http://schemas.microsoft.com/office/spreadsheetml/2010/11/ac"
    xmlns:ns3="http://schemas.microsoft.com/office/spreadsheetml/2014/revision"
    xmlns:ns4="http://schemas.microsoft.com/office/spreadsheetml/2016/revision6"
    xmlns:ns5="http://schemas.microsoft.com/office/spreadsheetml/2016/revision10"
    xmlns:ns6="http://schemas.microsoft.com/office/spreadsheetml/2015/revision2"
    xmlns:ns7="http://schemas.openxmlformats.org/officeDocument/2006/relationships"
    xmlns:ns8="http://schemas.microsoft.com/office/spreadsheetml/2010/11/main"
    xmlns:ns9="http://schemas.microsoft.com/office/spreadsheetml/2018/calcfeatures" ns1:Ignorable="x15 xr xr6 xr10 xr2">
    <ns0:fileVersion appName="xl" lastEdited="7" lowestEdited="7" rupBuild="23426" />
    <ns0:workbookPr defaultThemeVersion="166925" />
    <ns1:AlternateContent>
        <ns1:Choice Requires="x15">
            <ns2:absPath url="I:\Results\2021\" />
        </ns1:Choice>
    </ns1:AlternateContent>
    <ns3:revisionPtr revIDLastSave="0" documentId="13_ncr:1_{77E8B687-2130-4B59-8784-F53BD3BD2847}" ns4:coauthVersionLast="45" ns4:coauthVersionMax="45" ns5:uidLastSave="{00000000-0000-0000-0000-000000000000}" />
    <ns0:bookViews>
        <ns0:workbookView xWindow="28680" yWindow="-120" windowWidth="29040" windowHeight="15990" ns6:uid="{00000000-000D-0000-FFFF-FFFF00000000}" />
    </ns0:bookViews>
    <ns0:sheets>
        <ns0:sheet name="Blad1" sheetId="1" ns7:id="rId1" />
    </ns0:sheets>
    <ns0:calcPr calcId="191029" />
    <ns0:extLst>
        <ns0:ext uri="{140A7094-0E35-4892-8432-C4D2E57EDEB5}">
            <ns8:workbookPr chartTrackingRefBase="1" />
        </ns0:ext>
        <ns0:ext uri="{B58B0392-4F1F-4190-BB64-5DF3571DCE5F}">
            <ns9:calcFeatures>
                <ns9:feature name="microsoft.com:RD" />
                <ns9:feature name="microsoft.com:Single" />
                <ns9:feature name="microsoft.com:FV" />
                <ns9:feature name="microsoft.com:CNMTM" />
                <ns9:feature name="microsoft.com:LET_WF" />
            </ns9:calcFeatures>
        </ns0:ext>
    </ns0:extLst>
</ns0:workbook>


from Change fileshare root in Excel dynamically by modifying the xml behind the Excel with Python

No comments:

Post a Comment