Ask A Cartographer

Make thumbnail using Python

September 02 2010 | 5 comments
Categories: Map Data

Hi,

Not sure if this is the right forum, but what code would I use in ArcGIS Desktop 10, using python to automatically make thumbnails of all of my MXD files.

Would this work:

import arcpy
mxd = arcpy.mapping.MapDocument(r"C:\Clients\2010\client1.mxd")
thumb = mxd.makeThmbnail(mxd)
del mxd

I would be doing a recursive folder search to then list every MXD, so that I could make thumbnails.

Regards...

Mapping Center Answer:

The syntax for creating thumbnails would be:  mxd.makeThumbnail (), as you guessed.

ArcGIS 10 has a new embedded Python window where you can test your simple code.

We are not sure if you are asking how to do a recursive search and update thumbnails for all the MXDs at once or if you just want the syntax for creating a single thumbnail. If it is just one thumbnail, then the code above will work. 

If it is the thumbnails for all the MXDs in a folder, I am providing you the code below that will loop through the provided folder (first level) and create the thumbnails for each MXD within that folder:

import arcpy, os
# Provide folder path to loop through (first level only)
folderPath = r"D:\MXD"
for filename in os.listdir(folderPath):
    fullpath = os.path.join(folderPath, filename)
    if os.path.isfile(fullpath):
        basename, extension = os.path.splitext(fullpath)
        if extension.lower() == ".mxd":
            mxd = arcpy.mapping.MapDocument(fullpath)
            print "creating thumbnail for " + fullpath
            mxd.makeThumbnail ()
            mxd.save()
del mxd

You can modify this as needed.

Hope this helps!

How do you deal with indents? posted by Christopher Dean on Mar 8 2011 8:53AM
I copied this code and modified it to do something slightly different without changing the formatting. My problem is I get a Python error telling me it expects an indent, then when I indent it says it is an unexpected indent. Any suggestions?
Loop posted by Christopher Dean on Mar 8 2011 11:29AM
I fixed my indentation issues but now it doesn't seem to be updating all my maps. I have 3 maps in a folder and it only updates the last one. I am adding a description to the map document properties.
Here is my code:
import arcpy, os
# Provide folder path to loop through (first level only)
folderPath = r"C:\TEMP\test"
for filename in os.listdir(folderPath):
fullpath = os.path.join(folderPath, filename)
if os.path.isfile(fullpath):
basename, extension = os.path.splitext(fullpath)
if extension.lower() == ".mxd":
mxdList = folderPath
for mxdPath in mxdList:
mxd = arcpy.mapping.MapDocument(fullpath)
mxd.description = "Python"
mxd.save()
del mxd
Similar coding for recursive folder searching and posted by Craig Poynter on Mar 8 2011 1:19PM
I wrote something similar late last year. Hope this might help. I am fairly sure it is working correctly.

def thumbnail():
import arcpy, os

folderPath = raw_input('Enter name of source folder: ')
folderPath = str(folderPath)
thePattern = '.mxd'

for root, dirs, files in os.walk(folderPath):
fileList = [os.path.join(root, fi) for fi in files if fi.endswith(thePattern)]
for item in fileList:
mxd = arcpy.mapping.MapDocument(item)
print 'Creating thumbnail for ' + item
arcpy.AddMessage('Creating thumbnail for ' + item)
mxd.deleteThumbnail()
mxd.makeThumbnail()
mxd.save()

del mxd

response = raw_input('\nDo you want to make more thumbnails es or o?: ')
response = response.upper()
if response == 'Y':
thumbnail()
elif response == 'N':
raw_input('\nPress the enter key to quit')

thumbnail()
code submitted lost indenting posted by Craig Poynter on Mar 8 2011 1:20PM
If you email me at cpoynter@csu.edu.au I will send you my code as all indenting was lost when submitted to the comments section.
re:Loop posted by Rajinder Nagi on Mar 8 2011 1:22PM
You need to modify only one line in the original code and it should work. I didn't get why you are using
mxdList = folderPath
for mxdPath in mxdList:


as for filename in os.listdir(folderPath) code line doing looping.

Instead change mxd.makeThumbnail () with mxd.description = "Python", where "Python" would be the description you want to give and it will work.

If you would like to post a comment, please login.

Contact Us | Legal | Privacy |