I think this should work (but I didn't have a chance to test).
Let me know how it does. Good luck!
import arcpy
arcpy.env.overwriteOutput = True
targetLayers=['farm fields','Municipalities'] #specify all the layers you want (they must have unique names)
mxdpath=r'C:\Path\To\MapDoc.mxd' #point to your mxd
mxd=arcpy.mapping.MapDocument("CURRENT")
lyrs=arcpy.mapping.ListLayers(mxd)
dfName='Layers' #name of the data frame you want to add the new grids to
for lyr in lyrs:
if not lyr.name in targetLayers:
continue #Skip all non-target layers
#set number of desired cells:
x=730
#set feature classes:
inFC=lyr
tempGrid=r'C:\Path\To\tempGrid.shp' #just a temporary file
outFC=r'C:\Path\To\grid_'+lyr.name+'.shp' #final grid
#delete tempGrid if exists:
if arcpy.Exists(tempGrid):
arcpy.Delete_management(tempGrid)
#set default grid variables:
extent = arcpy.Describe(inFC).extent
origin_coord=str(extent.XMin)+" "+str(extent.YMin)
oppositeCoorner=str(extent.XMax)+" "+str(extent.YMax)
y_axis_coord=str(extent.XMin)+" "+str(extent.YMin+10)
number_rows=number_columns=0
geometryType = 'POLYGON'
#set starting cellSize and increment:
cellSize=max(float(extent.width),float(extent.height))
inc=cellSize
countOutFC=0.0 #needed to start while loop
#Find an appropriate cell size:
while countOutFC!=x:
if cellSize==inc:
inc=inc/2.0
continue
cell_width=cell_height=cellSize
arcpy.CreateFishnet_management(tempGrid,origin_coord,y_axis_coord,cell_width,cell_height,number_rows,number_columns,oppositeCoorner,"LABELS","#",geometryType)
arcpy.MakeFeatureLayer_management(tempGrid, "temp_lyr")
arcpy.SelectLayerByLocation_management("temp_lyr", "INTERSECT", inFC)
countOutFC=float(arcpy.GetCount_management("temp_lyr").getOutput(0))
print "Cell size: "+str(cellSize)+" Count: "+str(countOutFC)
if countOutFC>x:
print "Overshot..."
cellSize=prevCellSize
inc=inc/2.0
else:
prevCellSize=cellSize
cellSize-=inc
cellSize=prevCellSize
print '\nFound an appropriate cell size: '+str(cellSize)
#Find a simplified cell size:
print "\nTrying to simplify..."
for i in range(len(str(cellSize))):
simpCellSize=round(cellSize,i)
if simpCellSize==0:
continue
cell_width=cell_height=simpCellSize
arcpy.CreateFishnet_management(tempGrid,origin_coord,y_axis_coord,cell_width,cell_height,number_rows,number_columns,oppositeCoorner,"LABELS","#",geometryType)
arcpy.MakeFeatureLayer_management(tempGrid, "temp_lyr")
arcpy.SelectLayerByLocation_management("temp_lyr", "INTERSECT", inFC)
countOutFC=float(arcpy.GetCount_management("temp_lyr").getOutput(0))
print "Cell size: "+str(simpCellSize)+" Count: "+str(countOutFC)
if countOutFC==x:
break
if simpCellSize==cellSize:
print "Could not easily find a simplified cell size."
#rerun for last good cellsize (not simplified):
cell_width=cell_height=cellSize
arcpy.CreateFishnet_management(tempGrid,origin_coord,y_axis_coord,cell_width,cell_height,number_rows,number_columns,oppositeCoorner,"LABELS","#",geometryType)
arcpy.MakeFeatureLayer_management(tempGrid, "temp_lyr")
arcpy.SelectLayerByLocation_management("temp_lyr", "INTERSECT", inFC)
else:
print "A simplified cell size: "+str(simpCellSize)
arcpy.Select_analysis("temp_lyr",outFC)
#add new data to mxd
df = arcpy.mapping.ListDataFrames(mxd, dfName)[0]
newlayer = arcpy.mapping.Layer(outFC)
arcpy.mapping.AddLayer(df, newlayer, "AUTO_ARRANGE")
print "Done!!!"