Bulk convert text notes from uppercase to sentence case

Hi,
Trying to bulk convert notes from uppercase to sentence case and have had a go at a python script (I have 2 scripts, trying to figure out which one will work best). The script works when I get the item at a certain index but when I try to put the list into the python script it gives an error. Any ideas? Also if anyone thinks one script is better then the other let me know.



Script 1:

# Load the Python Standard and DesignScript Libraries
import sys
import clr
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *

clr.AddReference('RevitServices')
from RevitServices.Persistence import DocumentManager

doc = DocumentManager.Instance.CurrentDBDocument
uidoc = DocumentManager.Instance.CurrentUIApplication.ActiveUIDocument

# The inputs to this node will be stored as a list in the IN variables.

import re

def convert_to_sentence_case(text):
    sentences = re.split(r'(?<=[.!?])\s+', text)
    formatted_sentences = [sentence.capitalize() for sentence in sentences]
    return ' '.join(formatted_sentences)

# Example usage
input_text = IN[0]
result = convert_to_sentence_case(input_text)
OUT = result

Script 2:

# Python script to convert string to sentence case in Dynamo

# Input string
input_string = IN[0]

# Function to convert string to sentence case
def convert_to_sentence_case(input_string):
    # Check if the string is not empty
    if input_string:
        # Convert the string to lowercase and then capitalize the first letter of each sentence
        return '. '.join(map(str.capitalize, input_string.split('. ')))
    else:
        return "Please provide a non-empty string."

# Call the function and pass the input string
output = convert_to_sentence_case(input_string)

# Output the result
OUT = output

You need to make your code iterate over a list of inputs

input_text = IN[0]
result = []
for i in input_text:    
    result.append(convert_to_sentence_case(i))
OUT = result

Or a similar formatting for the second script. FYI the first script you can cull most of the imports/references/document manager/etc.

As for which version is best, I’m generally a fan of Regex as I think it has great flexibility to handle edge cases. How do these handle multi-line text versus single line? Tabs? Weird people who double space the start of their sentences? Does a URL in the text cause problems? Simple text like what you’re dealing with there’s likely no difference, I just like regex as if/when those edge cases pop up I can just make a quick tweak and it’s sorted.

1 Like

Thanks, that works good for simple notes. I did try different instances of notes to test and it does remove any gaps from ‘enter’ and ‘tab’ so not ideal for that situation.

Here is the code for others who want to use it:

import re

def convert_to_sentence_case(text):
    sentences = re.split(r'(?<=[.!?])\s+', text)
    formatted_sentences = [sentence.capitalize() for sentence in sentences]
    return ' '.join(formatted_sentences)

input_text = IN[0]
result = []
for i in input_text:    
    result.append(convert_to_sentence_case(i))
OUT = result