<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Execute Python at MXD Open? in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/execute-python-at-mxd-open/m-p/609067#M47542</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Link to your Ideas post:&lt;/SPAN&gt;&lt;BR /&gt;&lt;A href="http://ideas.arcgis.com/ideaView?id=087E00000004rSJIAY"&gt;http://ideas.arcgis.com/ideaView?id=087E00000004rSJIAY&lt;/A&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Thu, 29 Aug 2013 14:48:48 GMT</pubDate>
    <dc:creator>MattSayler</dc:creator>
    <dc:date>2013-08-29T14:48:48Z</dc:date>
    <item>
      <title>Execute Python at MXD Open?</title>
      <link>https://community.esri.com/t5/python-questions/execute-python-at-mxd-open/m-p/609064#M47539</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Is it possible to have Python code execute in ArcMap after an MXD has opened?&amp;nbsp; 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. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Any ideas?&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 28 Aug 2013 10:57:33 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/execute-python-at-mxd-open/m-p/609064#M47539</guid>
      <dc:creator>EricMahaffey</dc:creator>
      <dc:date>2013-08-28T10:57:33Z</dc:date>
    </item>
    <item>
      <title>Re: Execute Python at MXD Open?</title>
      <link>https://community.esri.com/t5/python-questions/execute-python-at-mxd-open/m-p/609065#M47540</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;You could create a &lt;/SPAN&gt;&lt;A href="http://resources.arcgis.com/en/help/main/10.1/index.html#/Application_Extension/014p00000019000000/"&gt;Python Add-In and trap the newDocument/openDocument events&lt;/A&gt;&lt;SPAN&gt;.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 28 Aug 2013 17:30:54 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/execute-python-at-mxd-open/m-p/609065#M47540</guid>
      <dc:creator>JasonScheirer</dc:creator>
      <dc:date>2013-08-28T17:30:54Z</dc:date>
    </item>
    <item>
      <title>Re: Execute Python at MXD Open?</title>
      <link>https://community.esri.com/t5/python-questions/execute-python-at-mxd-open/m-p/609066#M47541</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thanks Jason.&amp;nbsp; That's exactly what I ended up doing yesterday.&amp;nbsp; I wrote a Python AddIn Extension that runs the arcpy.mapping.PrintMap function right before the document closes.&amp;nbsp; This allows the MXD to open and fully assemble the geometry and tables before producing the PDF file.&amp;nbsp; 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.&amp;nbsp; All this will be attached to a scheduled task so that the data and masp are updated on a regular basis.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;This is a heck of a work around for bugs (NIM062177 and NIM063441).&amp;nbsp; Supposedly my "Ideas" posting about them has to get a lot of attention in order for the issue to be fixed.&amp;nbsp; 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.&amp;nbsp; Just search for "Export to PDF Drops Embeded Attribute Tables".&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 29 Aug 2013 09:50:00 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/execute-python-at-mxd-open/m-p/609066#M47541</guid>
      <dc:creator>EricMahaffey</dc:creator>
      <dc:date>2013-08-29T09:50:00Z</dc:date>
    </item>
    <item>
      <title>Re: Execute Python at MXD Open?</title>
      <link>https://community.esri.com/t5/python-questions/execute-python-at-mxd-open/m-p/609067#M47542</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Link to your Ideas post:&lt;/SPAN&gt;&lt;BR /&gt;&lt;A href="http://ideas.arcgis.com/ideaView?id=087E00000004rSJIAY"&gt;http://ideas.arcgis.com/ideaView?id=087E00000004rSJIAY&lt;/A&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 29 Aug 2013 14:48:48 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/execute-python-at-mxd-open/m-p/609067#M47542</guid>
      <dc:creator>MattSayler</dc:creator>
      <dc:date>2013-08-29T14:48:48Z</dc:date>
    </item>
    <item>
      <title>Re: Execute Python at MXD Open?</title>
      <link>https://community.esri.com/t5/python-questions/execute-python-at-mxd-open/m-p/609068#M47543</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;OK, well I managed to get the code to execute after the MXD opens using an AddIn Extension.&amp;nbsp; However, the code runs before the MXD can fully assemble the layout.&amp;nbsp; It's running the PrintToPDF function so I need the layout to be complete before it runs so that the PDF generates correctly.&amp;nbsp; I've tried using time.sleep to stall it while the file opens, but that doesn't seem to work.&amp;nbsp; It seems to just stall the actual opening of the file.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;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?&amp;nbsp; Here's the code I'm trying:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;import arcpy&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;import pythonaddins&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;import os&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;import time&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;class ExtensionClass1(object):&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; """Implementation for PrintToPDF_AddIn_addin.extension2 (Extension)"""&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; def __init__(self):&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # For performance considerations, please remove all unused methods in this class.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; self.enabled = True&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; def openDocument(self): &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; time.sleep(10)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; mxd = arcpy.mapping.MapDocument("CURRENT")&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if mxd.description == 'Print to PDF':&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.mapping.PrintMap(mxd, r"Adobe PDF", "PAGE_LAYOUT") &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; pass&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 03 Sep 2013 11:47:01 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/execute-python-at-mxd-open/m-p/609068#M47543</guid>
      <dc:creator>EricMahaffey</dc:creator>
      <dc:date>2013-09-03T11:47:01Z</dc:date>
    </item>
    <item>
      <title>Re: Execute Python at MXD Open?</title>
      <link>https://community.esri.com/t5/python-questions/execute-python-at-mxd-open/m-p/609069#M47544</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;A href="http://forums.arcgis.com/threads/48475-Please-read-How-to-post-Python-code" rel="nofollow noopener noreferrer" target="_blank"&gt;http://forums.arcgis.com/threads/48475-Please-read-How-to-post-Python-code&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;See if doing a refresh before printing fixes it:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
import arcpy
import pythonaddins
import os
import time


class ExtensionClass1(object):
&amp;nbsp;&amp;nbsp;&amp;nbsp; """Implementation for PrintToPDF_AddIn_addin.extension2 (Extension)"""
&amp;nbsp;&amp;nbsp;&amp;nbsp; def __init__(self):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # For performance considerations, please remove all unused methods in this class.
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; self.enabled = True


&amp;nbsp;&amp;nbsp;&amp;nbsp; def openDocument(self): 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; mxd = arcpy.mapping.MapDocument("CURRENT")

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; mxd.activeView = "PAGE_LAYOUT" #Make sure Layout View is active
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.RefreshActiveView() #Refresh the view

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if mxd.description == 'Print to PDF':
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.mapping.PrintMap(mxd, r"Adobe PDF", "PAGE_LAYOUT") 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; pass
&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 02:04:18 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/execute-python-at-mxd-open/m-p/609069#M47544</guid>
      <dc:creator>MattSayler</dc:creator>
      <dc:date>2021-12-12T02:04:18Z</dc:date>
    </item>
    <item>
      <title>Re: Execute Python at MXD Open?</title>
      <link>https://community.esri.com/t5/python-questions/execute-python-at-mxd-open/m-p/609070#M47545</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thanks for the suggestion Matt.&amp;nbsp; I tried the arcpy.RefreshActiveView, but it didn't seem to work.&amp;nbsp; 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.&amp;nbsp; 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).&amp;nbsp; I just have to figure out how to close the MXD (or ArcMap).&amp;nbsp; I've tried "killing" the app, but it ends up trying to shtudown before the MXD has opened.&amp;nbsp; Even with a time.sleep() added to stall it.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import arcpy
import pythonaddins
import os
import time


class ExtensionClass1(object):
&amp;nbsp;&amp;nbsp;&amp;nbsp; """Implementation for PrintToPDF_AddIn_addin.extension2 (Extension)"""
&amp;nbsp;&amp;nbsp;&amp;nbsp; def __init__(self):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # For performance considerations, please remove all unused methods in this class.
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; self.enabled = True
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; def beforeCloseDocument(self):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; #this works but runs before file fully opens
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; mxd = arcpy.mapping.MapDocument("CURRENT")
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if mxd.description == 'Print to PDF':
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.mapping.PrintMap(mxd, r"Adobe PDF", "PAGE_LAYOUT")
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; time.sleep(25)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; os.system('taskkill /IM Arcmap*')&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 02:04:21 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/execute-python-at-mxd-open/m-p/609070#M47545</guid>
      <dc:creator>EricMahaffey</dc:creator>
      <dc:date>2021-12-12T02:04:21Z</dc:date>
    </item>
    <item>
      <title>Re: Execute Python at MXD Open?</title>
      <link>https://community.esri.com/t5/python-questions/execute-python-at-mxd-open/m-p/609071#M47546</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;What's the overall workflow? Is the user interacting with the map or are you just printing each mxd?&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 04 Sep 2013 16:20:29 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/execute-python-at-mxd-open/m-p/609071#M47546</guid>
      <dc:creator>MattSayler</dc:creator>
      <dc:date>2013-09-04T16:20:29Z</dc:date>
    </item>
    <item>
      <title>Re: Execute Python at MXD Open?</title>
      <link>https://community.esri.com/t5/python-questions/execute-python-at-mxd-open/m-p/609072#M47547</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;What's the overall workflow? Is the user interacting with the map or are you just printing each mxd?&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Oops, missed the reference to the bugs. That &lt;/SPAN&gt;&lt;SPAN style="font-style:italic;"&gt;is &lt;/SPAN&gt;&lt;SPAN&gt;a heck of a work around. I wonder if &lt;/SPAN&gt;&lt;A href="http://www.reportlab.com/software/opensource/rl-toolkit/"&gt;ReportLab&lt;/A&gt;&lt;SPAN&gt; 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.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 04 Sep 2013 20:23:57 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/execute-python-at-mxd-open/m-p/609072#M47547</guid>
      <dc:creator>MattSayler</dc:creator>
      <dc:date>2013-09-04T20:23:57Z</dc:date>
    </item>
    <item>
      <title>Re: Execute Python at MXD Open?</title>
      <link>https://community.esri.com/t5/python-questions/execute-python-at-mxd-open/m-p/609073#M47548</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Matt,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Yea I looked at Report Labs a couple of years ago when I first started with this mess.&amp;nbsp; It seemed to be way to complicated just get some tables into a PDF.&amp;nbsp; 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.&amp;nbsp; In my case I have ten tables in my layout.&amp;nbsp; SO the task would be too difficult.&amp;nbsp; 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.&amp;nbsp; Obviously it's not.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 05 Sep 2013 10:02:30 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/execute-python-at-mxd-open/m-p/609073#M47548</guid>
      <dc:creator>EricMahaffey</dc:creator>
      <dc:date>2013-09-05T10:02:30Z</dc:date>
    </item>
  </channel>
</rss>

