[GIS] Exporting mxd using ArcPy/Python without opening it

arcgis-10.2arcpyexportpdf

I would like to be able to update a load of pdf and jgeg maps once the data has been updated. Currently people have to open each mxd and export the map in each format. I've written a short python script that will export the current mxd. This works well:

##export current mxd to pdf and jpeg
mxd = arcpy.mapping.MapDocument("CURRENT")
mxd.save()
filename = mxd.filePath.split("\\")[-1].split(".")[0]
path = '\\\\main.corp.local\\E-GB$\\Home\\4\\J042\\Documents\\PWISE' + "\\" +filename
arcpy.mapping.ExportToPDF(mxd, path + '.pdf')
arcpy.mapping.ExportToJPEG(mxd, path + '.jpg',"PAGE_LAYOUT", 400,500,300)

This doesnt save all that much time though because someone still needs to open all the mxds and run it. I've therefore written this to run on each mxd:

##export current mxd to pdf and jpeg
import arcpy, os
#mxd = arcpy.mapping.MapDocument("CURRENT")
for root, dirs, files in os.walk(r"\\main.glb.corp.local\EP-GB$\Home\ABZ\4\J0422414\Desktop\EXPORTMXD"):

        for f in files:
            if f.endswith(".mxd"):
                mxdfile = root + '\\' + f
                mxd = arcpy.mapping.MapDocument(mxdfile)
                mxd.save()
                filename = mxd.filePath.split("\\")[-1].split(".")[0]
                path = '\\\\main.corp.local\\E-GB$\\Home\\4\\J042\\Documents\\PWISE' + "\\" +filename
                arcpy.mapping.ExportToPDF(mxd, path + '.pdf')
                arcpy.mapping.ExportToJPEG(mxd, path + '.jpg',"PAGE_LAYOUT", 400,500,300)
            else:
                print f

The weird problem here is that when there is a table in the layout view of the lap (as in attribute table menu > Add Table to Layout) it will not show – there is just a white space. This problem occurs both on the pdf and the jpeg

Does anyone know either if there is a solution to this export bug, or if I can adjust my script to open and close each mxd, running the relevant part of the script from within it? because it seems to work if the mxd is open

Best Answer

After some searching, it seems like this has been a 'known' issue/limitation since about 2012, unfortunately. See here: https://geonet.esri.com/thread/60621. The answer suggested some plugins to add the functionality.

An alternative for you, is to build your own table every time your script opens. This requires having a template graphic elements for a vertical and horizontal line, and a template text element in your document. Essentially you copy these templates to build your table and values.

There is an example with code on this help page: http://desktop.arcgis.com/en/arcmap/10.3/analyze/arcpy-mapping/graphicelement-class.htm