batch export jpeg with world files data driven pages

6572
16
06-16-2016 06:41 AM
AntoninGosset
New Contributor III

Hi there,

I'd like to know if it is possible to batch export jpg + world file (jpw or so) in batch using a Data Driven Pages python script.

I don't think it is possible because there is a limitation in Arcgis : the layout view doesn't permit the world file attachement and Data Driven Pages only exports layout views.

World file creation is only allowed in Data view.

I don't know if there is a trick / solution to do what I want to achieve using an advanced python script. A script that would run data driven pages in Layout View to export each view as JPG+ world file ?

Thanks

Example of python script to export Data driven pages as EMF in Data view

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

for pageNum in range(1, mxd.dataDrivenPages.pageCount + 1):

  mxd.dataDrivenPages.currentPageID = pageNum

  arcpy.mapping.ExportToEMF(mxd, r"J:\81B\____Répertoires Trafic\60-Contributions_autres_agences\62-Production\Paris\16_RFF_SCH_SECTEUR\3_production\A_SIG_SchSect\PRODCARTO\11_Prospective\Carte_" + str(pageNum) + ".emf", convert_markers=True)

  del mxd      

0 Kudos
16 Replies
DanPatterson_Retired
MVP Emeritus

ExportToJpeg does allow for world files So I am not sure what you are trying to export

0 Kudos
AntoninGosset
New Contributor III

Yes it does ... only for the data view as I said.

The data driven pages batch export only works with layout views, thus preventing to write world files ... so I'm stuck.

Thanks

0 Kudos
FC_Basson
MVP Regular Contributor

I think Dan is right, you can export the layout view to JPEG with a world file according to the documentation: ExportToJPEG—Help | ArcGIS for Desktop

ExportToJPEG (map_document, out_jpeg, {data_frame}, {df_export_width}, {df_export_height}, {resolution}, {world_file}, {color_mode}, {jpeg_quality}, {progressive})

And here is some code to iterate through your data driven pages:

mxd = arcpy.mapping.MapDocument("CURRENT")
# Iterate map series pages
for pageNum in range(1, mxd.dataDrivenPages.pageCount + 1):
   mxd.dataDrivenPages.currentPageID = pageNum
   outFile = outputFolder + "\\" + mxd.dataDrivenPages.pageRow.getValue(mxd.dataDrivenPages.pageNameField.name) + ".jpg"
   arcpy.mapping.ExportToJPEG(mxd, outFile, resolution=150, world_file=True)
AntoninGosset
New Contributor III

Thank you very much.

I'll test the code. I'm not the only one in my company who's interested to do it. It'll help us a lot.

Cheers

0 Kudos
AntoninGosset
New Contributor III

I ran a test but you know ... I'm don't know anything in python. I tried place my output files in C:\Export. Can you tell me what syntax should I use ?

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

... # Iterate map series pages

... for pageNum in range(1, mxd.dataDrivenPages.pageCount + 1):

...    mxd.dataDrivenPages.currentPageID = pageNum

...    outFile = "C:\Export" + "\\" + mxd.dataDrivenPages.pageRow.getValue(mxd.dataDrivenPages.pageNameField.name) + ".jpg"

...    arcpy.mapping.ExportToJPEG(mxd, outFile, resolution=150, world_file=True)

...   

Runtime error

Traceback (most recent call last):

  File "<string>", line 5, in <module>

TypeError: cannot concatenate 'str' and 'int' objects

0 Kudos
FC_Basson
MVP Regular Contributor

There's an error in your pathname.  Luckily Dan Patterson​ wrote a real nice blog post on Python path names, which I would advise any novice Pythonian to take note of: Filenames and file paths in Python.

To mix strings and integers, use the str() function on the integers:

>>> "abc" + 123
Runtime error 
Traceback (most recent call last):
  File "<string>", line 1, in <module>
TypeError: cannot concatenate 'str' and 'int' objects
>>> "abc" + str(123)
'abc123'
>>> 
0 Kudos
DanPatterson_Retired
MVP Emeritus

When you are learning don't take short cuts

a = mxd.dataDrivenPages.pageNameField.name

print(a)

b = r "C:\Export" + "\\" + str(mxd.dataDrivenPages.pageRow.getValue(a)) + ".jpg"

print(b)

and see if it print out correctly

0 Kudos
AntoninGosset
New Contributor III

I tried but failed. Could you write the right code ? Thank you

0 Kudos
DanPatterson_Retired
MVP Emeritus

what were the print statement results? and what were the errors?

0 Kudos