4

I'm trying to remove a certain Table (xlsx-File) from some MXD-Files using python, but I can't finguring out how.

I tried the code sniplet for removing:

import arcpy, os
from arcpy import env

arcpy.env.workspace = 'V:/Projects/ZEL'

mxd =arcpy.mapping.MapDocument('V:/Projects/ZEL/test04.mxd')

df=arcpy.mapping.ListDataFrames(mxd, 'u')[0]

for lyr in arcpy.mapping.ListLayers(mxd, '', df):
    if lyr.name.lower()=='header_vorlage$':
        if lyr.dataSource == 'V:\Projects\ZEL\SHP\Daten_111004\ZEL_LB_Tabelle_V3_2010_2030.xlsx':
            arcpy.mapping.RemoveLayer(df, lyr)


mxd.saveACopy('pt.mxd')

print 'fertig'

del mxd

It works, but when I open the new created 'pt.mxd' there is still the symbol of the xlsx-Table with an '!' and you have the option to fix the datasource, because I have removed the path, but not the table...

I hope its not too confusing :)

My question is: Is there a way to remove a xlsx-Sheet from MXD-File without opening?

1
  • Table views are listed seprately through arcpy.mapping.ListTableViews Aug 12, 2016 at 16:36

1 Answer 1

1

The reason your script is not finding and removing your table is that it is a Table View, not a Layer. As such you need to use arcpy.mapping.ListTableViews to find it, and then arcpy.mapping.RemoveTableView to remove it from your MXD.

Because you're searching through a list of Layers, it never finds it so doesn't attempt to remove it.

Try the following:

import arcpy, os

arcpy.env.workspace = r'V:\Projects\ZEL'

mxd = arcpy.mapping.MapDocument(r'V:\Projects\ZEL\test04.mxd')

df = arcpy.mapping.ListDataFrames(mxd, 'u')[0]

for tbl in arcpy.mapping.ListTableViews(mxd, '', df):
    if tbl.name.lower() == 'header_vorlage$':
        if tbl.dataSource.lower() == r'v:\projects\zel\shp\daten_111004\zel_lb_tabelle_v3_2010_2030.xlsx\header_vorlage$':
            arcpy.mapping.RemoveTableView(df, tbl)

mxd.saveACopy('pt.mxd')

print 'fertig'

del mxd

You will see I've used tbl.dataSource.lower() and made that string entirely lower case while adding the Sheet Name to the end of that string. Once that Data Source is found it will remove that Excel table from your MXD.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.