AnsweredAssumed Answered

Physically open a file using Arcpy

Question asked by PChristensenGIS on Oct 18, 2018
Latest reply on Oct 18, 2018 by bixb0012

I have a script that runs the "Tabulate Intersection" tool for me that is assigned to a toolbar button. It works wonderfully. 

 

This is the first script I have created so I apologize if it is not as clean as it can be! 

 

My last step in this map automation, is to open the excel file once it is created instead of me having to navigate all the way to its location in the file explorer just to open it. That takes away the simplicity of just clicking a button and having the desired output.

 

One option is to just have the saved destination in a folder on my desktop that way it is just two clicks and I am there. But I wanted to first see if there was a way in arcpy to actually open a document in its native program (Excel) for viewing.

 

 

import arcpy
import pythonaddins

class TabulateSoil(object):
    """Implementation for Tools_addin.button (Button)"""
    def __init__(self):
        self.enabled = True
        self.checked = False
    def onClick(self):
       
        # Set current map document
        mxd = arcpy.mapping.MapDocument("CURRENT")
       
        # Remove existing "Soil_Tabulate" from TOC
        for df in arcpy.mapping.ListDataFrames(mxd):
            for tbl in arcpy.mapping.ListTableViews(mxd,"",df):
                if tbl.name == "Soil_Tabulate":
                    arcpy.mapping.RemoveTableView(df, tbl)
                   
        # Define parameters
        table = r'C:\Users\pchristensen\Documents\ARCGIS\Default.gdb\Soil_Tabulate'
        in_table = r'c:\users\pchristensen\Documents\ARCGIS\Default.gdb\Soil_Tabulate'
        out_xls = r'C:\Users\pchristensen\Documents\ARCGIS\scratch\Soiltab.xls'

        # Delete "table"
        arcpy.Delete_management(table)
       
        # Run Tabulate Intersection tool against selected parcel polygon
        arcpy.TabulateIntersection_analysis("Parcel Polygon","TMS","Soil Polys", r"C:\Users\pchristensen\Documents\ARCGIS\Default.gdb\Soil_Tabulate", "MUSYM")
       
        # Run Table to Excel
        arcpy.Delete_management(out_xls)
        arcpy.TableToExcel_conversion(in_table,out_xls)
       
        del table
        del in_table
        del out_xls
        del mxd
        pass

Outcomes