Get TitleBlock from ViewSheet

# -*- coding: utf-8 -*-

#⬇️ IMPORTS
from Autodesk.Revit.DB import  *

#📦 VARIABLES
doc      = __revit__.ActiveUIDocument.Document
app      = __revit__.Application
rvt_year = int(app.VersionNumber)

# 👇 FUNCTION
def get_titleblocks_from_sheet(sheet, doc):
    # type:(ViewSheet, Document) -> list
    """Function to get TitleBlocks from the given ViewSheet.
    :param sheet: ViewSheet that has TitleBlock
    :param doc:   Document instance of the Project
    :return:      list of TitleBlocks that are placed on the given Sheet."""

    # RULE ARGUMENTS
    rule_value         = sheet.SheetNumber
    param_sheet_number = ElementId(BuiltInParameter.SHEET_NUMBER)
    f_pvp              = ParameterValueProvider(param_sheet_number)
    evaluator          = FilterStringEquals()

    # CREATE A RULE (Method has changed in Revit API in 2022)
    if rvt_year < 2022:
        f_rule = FilterStringRule(f_pvp, evaluator, rule_value, True)  # RVT 2021-
    else:
        f_rule = FilterStringRule(f_pvp, evaluator, rule_value)        # RVT 2022+

    # CREATE A FILTER
    tb_filter = ElementParameterFilter(f_rule)

    # GET TITLEBLOCKS
    tb = FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_TitleBlocks) \
        .WhereElementIsNotElementType().WherePasses(tb_filter).ToElements()

    return list(tb)


#🎯 MAIN
if __name__ == '__main__':
    # GET RANDOM SHEET
    random_sheet = FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_Sheets)\
        .WhereElementIsNotElementType().FirstElement()
 
    # GET TITLEBLOCK
    title_block = get_titleblocks_from_sheet(random_sheet, doc)

__author__ = '🙋‍♂️ Erik Frits'

⌨️ Happy Coding!
Erik Frits