Python script in export pdf with fieldname

744
5
10-12-2018 08:43 PM
ChanYuk_Yu
Emerging Contributor

Hi, I have the script below. I am trying to export 400 pdfs with the fieldname "Label" but it only export the last page of pdf. What goes wrong ?

import arcpy

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

pageNameField = "Label" 

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

        mxd.dataDrivenPages.currentPageID = pageNum

pageName = mxd.dataDrivenPages.pageRow.getValue(pageNameField)

pageName = pageName.replace("/", "_")

pageName = pageName.replace(" ", "")

print "Exporting page {0} of {1}".format(str(mxd.dataDrivenPages.currentPageID), str(mxd.dataDrivenPages.pageCount))

mxd.dataDrivenPages.refresh()

 

arcpy.mapping.ExportToPDF(mxd, r"C:\TEST\181013_test" + str(pageName) + ".pdf")

del mxd

0 Kudos
5 Replies
DanPatterson_Retired
MVP Emeritus

/blogs/dan_patterson/2016/08/14/script-formatting 

There seem to be a few issues with code formatting that might be real or due to copying.

Throw in a couple of print statements to see what the page and output filename are printing as you go along. 

You don't have to go through all 400, just check

print("page count {}".format(mxd.dataDrivenPages.pageCount))

And change this line to....

for pageNum in range(1, mxd.dataDrivenPages.pageCount + 1):‍  # ---- original


for pageNum in range(1, 5):  # ---- or some small number for testing
ChanYuk_Yu
Emerging Contributor

Hi Dan

I am new in python, so I am copying and combined the sample or code posted to try to get a workaround.

Thank you for your suggestion. I tried range (1,5), the small test shows it only process the last page. I wonder if the problem comes from pageName because the code works all right when the second last script ExportToPdf is pageNum.

0 Kudos
DanPatterson_Retired
MVP Emeritus

that could be it, but I you didn't post print statement to confirm

0 Kudos
ChanYuk_Yu
Emerging Contributor

I inserted the print code you recommended and another error comes up..  What goes wrong at Line 11 ?

Parsing error SyntaxError: EOL while scanning string literal (line 11)

Here's the code

import arcpy

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

pageNameField = "Label" 

for pageNum in range(1, 5):

        mxd.dataDrivenPages.currentPageID = pageNum

pageName = mxd.dataDrivenPages.pageRow.getValue(pageNameField)

pageName = pageName.replace("/", "_")

pageName = pageName.replace(" ", "")

print("page count {}".format(mxd.dataDrivenPages.pageCount))

mxd.dataDrivenPages.refresh()

arcpy.mapping.ExportToPDF(mxd, r"D:\TEMP\" + str(pageName) + ".pdf")

del mxd

0 Kudos
DanPatterson_Retired
MVP Emeritus

You shouldn't use backslashes to end a path.

Also, check the link I sent on Code Formatting, especially if you want to refer to line numbers

a = r"D:\TEMP\"
  File "<ipython-input-1-c1bcefbc921d>", line 1
    a = r"D:\TEMP\"
                   ^
SyntaxError: EOL while scanning string literal


a = r"D:\TEMP" + "\\somefile" +".pdf"  # ---- or

"{}\\{}{}".format(r"D:\temp", "somefile", ".pdf")
'D:\\temp\\somefile.pdf'
0 Kudos