Text Note Formatting using Python

Is it possible to turn the text before " : " of each line into bold through dynamo or python? It’s a textnote…

Here is an example of the textnote

And this is how I wish it turns into…

@tiagofrbr Yes, it’s possible using this method.
Try this:

import clr
clr.AddReference('RevitAPI')
from Autodesk.Revit.DB import *

clr.AddReference('RevitServices')
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
doc = DocumentManager.Instance.CurrentDBDocument

toList = lambda x : x if hasattr(x, '__iter__') else [x]
textNotes = toList(UnwrapElement(IN[0]))
OUT = []

TransactionManager.Instance.EnsureInTransaction(doc)
for textNote in textNotes:
	noteText = textNote.Text
	if IN[1] in noteText:
		end = noteText.index(IN[1])
		gft = textNote.GetFormattedText()
		gft.SetBoldStatus(TextRange(0,end+1), True)
		textNote.SetFormattedText(gft)
	OUT.append(textNote)
TransactionManager.Instance.TransactionTaskDone()
2 Likes

Thanks AmolShah! However it worked only for the first line… how can I make it work also for the other lines?

@tiagofrbr In that case try this: Bold.dyn (17.9 KB)

import clr
clr.AddReference('RevitAPI')
from Autodesk.Revit.DB import *

clr.AddReference('RevitServices')
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
doc = DocumentManager.Instance.CurrentDBDocument

toList = lambda x : x if hasattr(x, '__iter__') else [x]
textNotes = toList(UnwrapElement(IN[0]))
textNote = textNotes[0]

TransactionManager.Instance.EnsureInTransaction(doc)
for i in IN[1][:-1]:
	gft = textNote.GetFormattedText()
	gft.SetBoldStatus(TextRange(i,IN[2]),True)
	textNote.SetFormattedText(gft)
TransactionManager.Instance.TransactionTaskDone()

OUT = textNote

Python node inputs:
IN[1] : The start position of each new line
IN[2] : The number of characters you want to bold out from the start of line

4 Likes

Hello
another solution

import clr
import re
clr.AddReference('RevitAPI')
from Autodesk.Revit.DB import *

clr.AddReference('RevitServices')
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
doc = DocumentManager.Instance.CurrentDBDocument

toList = lambda x : x if hasattr(x, '__iter__') else [x]
textNotes = toList(UnwrapElement(IN[0]))

TransactionManager.Instance.EnsureInTransaction(doc)

for textNote in textNotes:
	gft = textNote.GetFormattedText()
	txt = textNote.Text 
	for match in  re.finditer(r'PL.+?\:', txt):
		lengthStr = match.end() - match.start()
		gft.SetBoldStatus(TextRange(match.start() ,lengthStr),True)
	textNote.SetFormattedText(gft)

TransactionManager.Instance.TransactionTaskDone()		
OUT = txt
2 Likes

Both methods worked! However c.poupin your method works only for the lines that start with PL? Because I’d like a code that works with any text before " : "… If possible I also need to clear the current format of the textnote before applying the bold

it is a regular expression, you can adapt it if necessary

1 Like

@tiagofrbr Try this: TextNote Formatting.dyn (11.0 KB)
TN

#Written by Amol S + Cyril P
import clr
import re
clr.AddReference('RevitAPI')
from Autodesk.Revit.DB import *

clr.AddReference('RevitServices')
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
doc = DocumentManager.Instance.CurrentDBDocument

toList = lambda x : x if hasattr(x, '__iter__') else [x]
textNotes = toList(UnwrapElement(IN[0]))
pattern = r'^.+?'+IN[1]

TransactionManager.Instance.EnsureInTransaction(doc)
for textNote in textNotes:
	gft = textNote.GetFormattedText()
	if IN[2]: gft.SetAllCapsStatus(False)
	if IN[3]: gft.SetBoldStatus(False)
	if IN[4]: gft.SetItalicStatus(False)
	if IN[5]: gft.SetUnderlineStatus(False)
	txt = textNote.Text
	reTxt = txt.replace("\r","\n").replace("\r\n","\n")
	for match in re.finditer(pattern,reTxt,re.M):
		lengthStr = match.end() - match.start()
		gft.SetBoldStatus(TextRange(match.start(),lengthStr),True)
	textNote.SetFormattedText(gft)
TransactionManager.Instance.TransactionTaskDone()		

OUT = IN[0]
2 Likes

AmolShah your last script works well except when I link also use another script to add the bullet. Do you know if there is a way to add the bullets for each line too in your python script?

@tiagofrbr Try this: TextNoteFormatting.dyn (12.4 KB)

#Written by Amol S + Cyril P
import clr
import re
clr.AddReference('RevitAPI')
from Autodesk.Revit.DB import *

clr.AddReference('RevitServices')
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
doc = DocumentManager.Instance.CurrentDBDocument

toList = lambda x : x if hasattr(x, '__iter__') else [x]
textNotes = toList(UnwrapElement(IN[0]))
pattern = r'^.+?'+IN[1]

TransactionManager.Instance.EnsureInTransaction(doc)
for textNote in textNotes:
	gft = textNote.GetFormattedText()
	if IN[2]: gft.SetAllCapsStatus(False)
	if IN[3]: gft.SetBoldStatus(False)
	if IN[4]: gft.SetItalicStatus(False)
	if IN[5]: gft.SetUnderlineStatus(False)
	if IN[6]:
		textRange = gft.AsTextRange()
		gft.SetListType(textRange,ListType.Bullet)
	txt = textNote.Text
	reTxt = txt.replace("\r","\n").replace("\r\n","\n")
	for match in re.finditer(pattern,reTxt,re.M):
		lengthStr = match.end() - match.start()
		gft.SetBoldStatus(TextRange(match.start(),lengthStr),True)
	textNote.SetFormattedText(gft)
TransactionManager.Instance.TransactionTaskDone()		

OUT = IN[0]
1 Like

Amazing it worked! Thanks AmolShah!

1 Like

AmolShah actually I realized that another problem happens sometimes. When the node TextNote.SetText is connected to the Select.element node all the text gets bold and it doesn’t work as expected. Do you know why?

1 Like

@tiagofrbr Connect the output of TextNote.SetText node to IN[7] input of Python node.
This way you make sure that the text is set first and then it’s being formatted by the Python node.