1

I am building a script that exports pdfs based on user input, each pdf will need a different date (not current) that the data was updated per the data driven attribute. The script runs fine but is only taking the last user input. I'm getting stumped in trying to figure out how to make the element text change with each pdf that is exporting.

# Export maps to pdf
ddp = mxd.dataDrivenPages
pageNumbers = sys.argv[5]
pgIndex = ddp.currentPageID
text_elements = arcpy.mapping.ListLayoutElements(mxd,"TEXT_ELEMENT")

for pageNum in pageNumbers.split(", "):
    ddp.currentPageID = int(pageNum)
    pageName = ddp.pageNameField.name
    for element in text_elements:
        if element.name == 'My Text Field':
            if (pgIndex == 1):
                element.text =  sys.argv[6]
    for element in text_elements:
        if element.name == 'My Text Field':
            if (pgIndex == 2):
                element.text =  sys.argv[7]
    for element in text_elements:
        if element.name == 'My Text Field':
            if (pgIndex == 3):
                element.text =  sys.argv[8]

    arcpy.mapping.ExportToPDF(mxd, r"C:\test\\"".pdf", image_quality="BEST", resolution="300")
mxd.save()
del mxd
1

Couple of things:

The output name for your pdf looks incorrect, you have:

r"C:\test\\"".pdf"

should it not be something like:

r"C:\test\\mytest_" + pageName + ".pdf"

The way you have your code indented means it loops through the for statement then exports the page layout, which will be your last map. I guess you want to export each map on each loop of your for loop? If so indent the ExportToPDF so that it is within the loop.

UPDATE:

Try replacing the lines:

if (pgIndex == 1):

with all their equivalents

if (pageNum == 1):
  • Sorry i forgot to indent that when i pasted the code, though i do have it indented for my code. – Ted Apr 1 '15 at 16:40
0

You can format your own dynamic text, assuming the date you want is an attribute of the data. Use the index layer attribute (or display expression) mentioned in the Page section near the bottom of the linked web page above.

<dyn type="page" property="attribute" field="<Field Name>" domainlookup="true"/>
  • I have some dynamic text in my mxds but the problem is the data set is so large that and always changing i would rather just have the users input the text through the python script. – Ted Apr 1 '15 at 19:23
  • How are you getting user input? GetParameterAsText? You can get as many inputs as you want, but do you want each page of the pdf to have a different date? That would be cumbersome using user input. – recurvata Apr 2 '15 at 12:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.