Execute Python at MXD Open?

2025
9
08-28-2013 03:57 AM
EricMahaffey
New Contributor III
Is it possible to have Python code execute in ArcMap after an MXD has opened?  I have a way to open an MXD through Python, but I then want another piece of code to execute once the MXD file has opened.

Any ideas?
Tags (2)
0 Kudos
9 Replies
JasonScheirer
Occasional Contributor III
0 Kudos
EricMahaffey
New Contributor III
Thanks Jason.  That's exactly what I ended up doing yesterday.  I wrote a Python AddIn Extension that runs the arcpy.mapping.PrintMap function right before the document closes.  This allows the MXD to open and fully assemble the geometry and tables before producing the PDF file.  Today I plan to put it all together in one script that opens the MXD, runs the extension to produce the PDF, and closes the MXD.  All this will be attached to a scheduled task so that the data and masp are updated on a regular basis.

This is a heck of a work around for bugs (NIM062177 and NIM063441).  Supposedly my "Ideas" posting about them has to get a lot of attention in order for the issue to be fixed.  I'd post the link, but there's no easy way to share (without having to generate yet another account) as far as I can tell.  Just search for "Export to PDF Drops Embeded Attribute Tables".
0 Kudos
MattSayler
Occasional Contributor II
0 Kudos
EricMahaffey
New Contributor III
OK, well I managed to get the code to execute after the MXD opens using an AddIn Extension.  However, the code runs before the MXD can fully assemble the layout.  It's running the PrintToPDF function so I need the layout to be complete before it runs so that the PDF generates correctly.  I've tried using time.sleep to stall it while the file opens, but that doesn't seem to work.  It seems to just stall the actual opening of the file.

Anyone have any ideas on how to get the arcpy.mapping.PrintMap function to run after the MXD fully opens, and assembles the map layout?  Here's the code I'm trying:

import arcpy
import pythonaddins
import os
import time


class ExtensionClass1(object):
    """Implementation for PrintToPDF_AddIn_addin.extension2 (Extension)"""
    def __init__(self):
        # For performance considerations, please remove all unused methods in this class.
        self.enabled = True

      
    def openDocument(self):
        time.sleep(10)
        mxd = arcpy.mapping.MapDocument("CURRENT")
        if mxd.description == 'Print to PDF':
            arcpy.mapping.PrintMap(mxd, r"Adobe PDF", "PAGE_LAYOUT")
        pass
0 Kudos
MattSayler
Occasional Contributor II
http://forums.arcgis.com/threads/48475-Please-read-How-to-post-Python-code

See if doing a refresh before printing fixes it:

import arcpy
import pythonaddins
import os
import time


class ExtensionClass1(object):
    """Implementation for PrintToPDF_AddIn_addin.extension2 (Extension)"""
    def __init__(self):
        # For performance considerations, please remove all unused methods in this class.
        self.enabled = True


    def openDocument(self): 
        mxd = arcpy.mapping.MapDocument("CURRENT")

        mxd.activeView = "PAGE_LAYOUT" #Make sure Layout View is active
        arcpy.RefreshActiveView() #Refresh the view

        if mxd.description == 'Print to PDF':
            arcpy.mapping.PrintMap(mxd, r"Adobe PDF", "PAGE_LAYOUT") 
        pass
0 Kudos
EricMahaffey
New Contributor III
Thanks for the suggestion Matt.  I tried the arcpy.RefreshActiveView, but it didn't seem to work.  With or without it, the PDF will build, however the tables end up printing out about a third of the original size in the PDF.  The only way that I've gotten it to work so far is if I run the arcpy.mapping.PrintMap function before the document closes (instead of at open).  I just have to figure out how to close the MXD (or ArcMap).  I've tried "killing" the app, but it ends up trying to shtudown before the MXD has opened.  Even with a time.sleep() added to stall it.

import arcpy
import pythonaddins
import os
import time


class ExtensionClass1(object):
    """Implementation for PrintToPDF_AddIn_addin.extension2 (Extension)"""
    def __init__(self):
        # For performance considerations, please remove all unused methods in this class.
        self.enabled = True
      
    def beforeCloseDocument(self):
        #this works but runs before file fully opens
        mxd = arcpy.mapping.MapDocument("CURRENT")
        if mxd.description == 'Print to PDF':
            arcpy.mapping.PrintMap(mxd, r"Adobe PDF", "PAGE_LAYOUT")
        time.sleep(25)
        os.system('taskkill /IM Arcmap*')
0 Kudos
MattSayler
Occasional Contributor II
What's the overall workflow? Is the user interacting with the map or are you just printing each mxd?
0 Kudos
MattSayler
Occasional Contributor II
What's the overall workflow? Is the user interacting with the map or are you just printing each mxd?


Oops, missed the reference to the bugs. That is a heck of a work around. I wonder if ReportLab could get around that. It's a python library with objects for creating pdf layouts and reports. Esri has used it in some of their arcpy.mapping examples, which is where I first heard of it. I haven't had an opportunity to use it yet, so I can't really provide anything concrete unfortunately.
0 Kudos
EricMahaffey
New Contributor III
Matt,

Yea I looked at Report Labs a couple of years ago when I first started with this mess.  It seemed to be way to complicated just get some tables into a PDF.  From what I remember the process involved building the tables piece by piece using graphics (lines) for the wireframe of the table, then inserting dynamic text into the spaces between the lines.  In my case I have ten tables in my layout.  SO the task would be too difficult.  I just wish that afetr several years of users submitting bug reports that ESRI would fix it instead of just rejecting the bug as a known software limitation, and saying that it's out of the scope of design.  Obviously it's not.
0 Kudos