Unable to save the MXD using external python script.

1074
6
07-03-2019 09:16 AM
krishmandali
New Contributor

I am writing a small automation to create a view, register the view in Geo database and publish it as a service. So far good with creating a view and register with Geo database but I am trying to save the mxd before publishing it and its not working out. I saving the registered view in following ways and not working

mxd = arcpy.mapping.MapDocument("C:\GIS\testfile.mxd") 

Error: AssertionError: Invalid MXD filename.

------------------------------------------------------------

mxd = arcpy.mapping.MapDocument("CURRENT")


Error : RuntimeError: Object: CreateObject cannot open map documen
0 Kudos
6 Replies
RandyBurton
MVP Alum

Did you try:

mxd = arcpy.mapping.MapDocument(r"C:\GIS\testfile.mxd")

The r before the quoted text of the file name will cause Python to interpret the string as raw text.  Otherwise, it would see the \t as an escaped tab character.

0 Kudos
krishmandali
New Contributor

@Randu Burton, Thanks for you're reply. I tried you're suggestion but its not working giving me the same error as "AssertionError: Invalid MXD filename.". 

Just for you're information there is no file with name "testfile.mxd" all I am trying to save the view as MXD which is not working.

0 Kudos
RandyBurton
MVP Alum

Then perhaps:

mxd.saveACopy(r"C:\GIS\testfile.mxd")‍‍

See MapDocument for more info as you can specify the version format.  There is also save().

0 Kudos
krishmandali
New Contributor

I am not even getting to that point. Creating MXD object itself is getting error.

0 Kudos
RandyBurton
MVP Alum

Perhaps if you could share a bit more of what you are doing would be helpful.

Here's a bit of an example of working with an mxd document (in this case to make adjustments to the visibility and transparency of some map layers):

import arcpy

mxd = arcpy.mapping.MapDocument(r"C:\Project\Project.mxd")
# or if inside ArcMap:
mxd = arcpy.mapping.MapDocument("CURRENT")

df = arcpy.mapping.ListDataFrames(mxd, "County Maps")[0]

# ListLayers (map_document_or_layer, {wildcard}, {data_frame})

for lyr in arcpy.mapping.ListLayers(mxd, "Lakes", df):
    if lyr.description == "USGS Lakes":
        lyr.visible = True
        lyr.transparency = 50

mxd.save() # save the document with changes
# or
mxd.saveACopy(r"C:\GIS\testfile.mxd") # save the changes to a new document

del mxd‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

In line 3 or 4, you are opening an mxd to work with.  This can be a previously saved document, or the current one opened in ArcMap.  Then make your changes to the document.   And in lines 16 or 18 you are saving the changes to either the original document or a new document.

0 Kudos
krishmandali
New Contributor

Hi Randy,

Here is my code

import arcpy

arcpy.env.workspace = r"Database Connections\Connection to SVTHCPHDW.sde"

arcpy.CreateDatabaseView_management("Database Connections\Connection to SVTHCPHDW.sde","Testagain","SELECT TOP (1000) [K_And_FW_key]       ,[zip]       ,[Shape]  ,[GeographyBaseline]       ,[Year]       ,[geography_key]       ,[DP05_0033]       ,[DP05_33E_EstNote]       ,[DP05_33E_Est]       ,[DP05_33E_ErrMgn]       ,[DP05_33E_ErrMgnNote]       ,[DP05_33PE_Pct]       ,[DP05_33PE_PctNote]       ,[DP05_33PE_PctErrMgn]       ,[DP05_33PE_PctErrMgnNote]       ,[DP05_0034]       ,[DP05_34E_EstNote]       ,[DP05_34E_Est]       ,[DP05_34E_ErrMgn]       ,[DP05_34E_ErrMgnNote]       ,[DP05_34PE_Pct]       ,[DP05_34PE_PctNote]       ,[DP05_34PE_PctErrMgn]       ,[DP05_34PE_PctErrMgnNote]       ,[DP05_0035]       ,[DP05_35E_EstNote]       ,[DP05_35E_Est]       ,[DP05_35E_ErrMgn]       ,[DP05_35E_ErrMgnNote]       ,[DP05_35PE_Pct]       ,[DP05_35PE_PctNote]       ,[DP05_35PE_PctErrMgn]       ,[DP05_35PE_PctErrMgnNote]   FROM [DWAcs].[rpt].[Pivot_ZipCode_Acs5DP05_GISTest]")
print "view created sucessfully"
inTable = r"Database Connections\Connection to SVTHCPHDW.sde\Testagain"
oid_field = "OID"
inTable = r"Database Connections\Connection to SVTHCPHDW.sde\Testagain"
oid_field = "K_And_FW_key"
shape_field = "Shape"
geometry_type = "POLYGON"
sr = arcpy.SpatialReference(4326)
in_extent = ""
arcpy.RegisterWithGeodatabase_management(inTable, oid_field, shape_field,
                                         geometry_type, sr, in_extent)

mxd = arcpy.mapping.MapDocument("CURRENT")//This one is creating error

I don't have mxd file previously saved in location. Need to save it after I registered the view with Geo Database.(on fly)

0 Kudos