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()
1 Answer 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.
-
2You need the carriage return
\r
because Windows EOL separator is CRLF - carriage return + line feed.Paul– Paul2016年04月15日 20:55:41 +00:00Commented 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.dslamb– dslamb2016年04月15日 21:40:17 +00:00Commented Apr 15, 2016 at 21:40
Title*
would return Title1 and Title2. desktop.arcgis.com/en/arcmap/10.3/analyze/arcpy-mapping/…