3

I am new to python, but I'm currently working on a script tool to loop through all MXDs within a folder and change their associated title block information(Map Title, Report Title, Date, Charge Code, etc...) then export the newly MXDs to PDF.

I have no problem changing some of the static information that is the same for all MXDs such as the Date/Client/Report through arcpy.GetParameterAsText, but my real question is ...

Is there a way to allow for multiple titles?

For instance I know you can allow for multi-values and the output string looks something like "Exhibit 1 ; Exhibit 2; Exhibit 3 " Is there a way to take that string and split it into individual titles for each based off the order of the mxds in the folder?

Here is my current script :

 env.workspace = r"E:/Python/ENV_TEST"
clientTitle = arcpy.GetParameterAsText(0)
reportTitle = arcpy.GetParameterAsText(1)
dateTitle = arcpy.GetParameterAsText(2)
PNTitle = arcpy.GetParameterAsText(3)
titleList = arcpy.GetParameterAsText(4)
for mxdname in arcpy.ListFiles("*.mxd"):
 mxd = arcpy.mapping.MapDocument(r"E:\Python\ENV_TEST\\" + mxdname)
 pdfname = mxdname + ".pdf" 
 for titleItem in arcpy.mapping.ListLayoutElements(mxd,"TEXT_ELEMENT", "Title"):
 titleItem.text = titleList
 for ReportItem in arcpy.mapping.ListLayoutElements(mxd,"TEXT_ELEMENT", "Report"):
 ReportItem.text = reportTitle
 for ClientItem in arcpy.mapping.ListLayoutElements(mxd,"TEXT_ELEMENT", "Client"):
 ClientItem.text = clientTitle
 for DateItem in arcpy.mapping.ListLayoutElements(mxd,"TEXT_ELEMENT", "Date"):
 DateItem.text = dateTitle
 for PNItem in arcpy.mapping.ListLayoutElements(mxd,"TEXT_ELEMENT", "PN"):
 PNItem.text = PNTitle
 arcpy.mapping.ExportToPDF(mxd,pdfname)
 mxd.save()
PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Apr 15, 2016 at 16:51
1

1 Answer 1

1

If I understand correctly, you have a text element in your layout called Title. You'd like to replace the text of the element with variable titleList. I assume the value of the variable looks like "Exhibit 1;Exhibit 2;Exhibit 3".

If you want all the titles in a single text element on different lines you could do something like this. The code below assumes that titlelist looks like a string with titles separated by a semicolon, and the text element area is large enough to support multiple lines of titles.

Swap the semicolons for carriage returns:

titlelist= titlelist.replace(";","\n")

Then replace the text:

titleItem.text = titleList

or this way to clean up any spaces:

splt = [x.strip() for x in titlelist.split(";")]
titleItem.text = "\n".join(splt)

You can also get more sophisticated using formatting tags so you can have subtitles:

splt = [x.strip() for x in titlelist.split(";")]
result='<FNT size = "12">{0}</FNT>\r\n<FNT size = "10">{1}</FNT>\r\n<FNT size = "10">{2}</FNT>\r\n'.format(splt[0],splt[1],splt[2])
titleItem.text = result
mxd.save()

I realize that isn't too flexible since it is hardcoded to three titles. You would need to loop through in some way. I found using 10.2.2 on my machine only using the \n doesn't create a carriage return you need \r\n for it to work. Edit: See @paul's comment for an explanation about \r and \n.

answered Apr 15, 2016 at 18:38
2
  • 2
    You need the carriage return \r because Windows EOL separator is CRLF - carriage return + line feed. Commented Apr 15, 2016 at 20:55
  • Thanks for the clarification. Why would only be a problem when I used the formatting tags? It didn't seem to matter when inserting the text. Commented Apr 15, 2016 at 21:40

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.