Edit the schedule column headers with Python

Hi,
I work on Python script that creates and edits schedules.
I wish to edit the headings of schedule columns - the row that contains the parameter names, below the schedule title. It can be overridden to any other text in UI. As far as I understand, it is the row no. 0 of the Body type of TableSectionData.

I can successfully edit the schedule title, however, when I try to edit the column headers I get the exception ā€œ This operation is forbidden for cells in standard schedule body sections.ā€.

Does it mean that you cannot really edit this row through API, even though you can edit it with UI?

Please see a part of my code (choosing any schedule in the model and trying to override first cell in the column heading).

import clr

clr.AddReference("ProtoGeometry")
from Autodesk.DesignScript.Geometry import *

# Import RevitNodes
clr.AddReference("RevitNodes")

import Revit
clr.ImportExtensions(Revit.Elements)
clr.ImportExtensions(Revit.GeometryConversion)

clr.AddReference("RevitAPI")

clr.AddReference("RevitAPIUI")

import Autodesk
from Autodesk.Revit.DB import *

# Import Revit elements
from Revit.Elements import *

# Import DocumentManager
clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
from System.Collections.Generic import *

import sys

doc = DocumentManager.Instance.CurrentDBDocument
uiapp = DocumentManager.Instance.CurrentUIApplication
app = uiapp.Application
adoc = doc.ActiveView


schedule = FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_Schedules).WhereElementIsNotElementType().FirstElement()

TransactionManager.Instance.EnsureInTransaction(doc)

tableData = schedule.GetTableData()
tableBody = schedule.GetTableData().GetSectionData(SectionType.Body)
tableBody.SetCellText(0, 0, "MY TEXT")

TransactionManager.Instance.TransactionTaskDone()

I managed to find solution on my own.
Editing the column headers is possible, but in a different way.
In case anyone would have similar struggle, I’m leaving solution: it worked for me to edit the ColumnHeading property of ScheduleField class, instead of trying to set the cell text.

https://www.revitapidocs.com/2018.2/3890f745-6f24-f81a-9f8f-d8b47c8e3f94.htm

This is how I got it working, just sharing for others:

import clr
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *

# Add necessary Revit API references
clr.AddReference("RevitAPI")
clr.AddReference("RevitServices")
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
from Autodesk.Revit.DB import *

# Access the current document
doc = DocumentManager.Instance.CurrentDBDocument

# Retrieve the list of ViewSchedules from Dynamo input
viewSchedules = [UnwrapElement(sched) for sched in IN[0]]

# Mapping dictionary for old headers to new headers
mappingDictOldNew = IN[1]

# Start a transaction to modify the document
TransactionManager.Instance.EnsureInTransaction(doc)

try:
    # Iterate over each ViewSchedule in the list
    for viewSchedule in viewSchedules:
        # Get the number of fields in each schedule's definition
        nFields = viewSchedule.Definition.GetFieldCount()
        for n in range(nFields):
            field = viewSchedule.Definition.GetField(n)
            # Get the current column header
            currentHeader = field.ColumnHeading
            if currentHeader in mappingDictOldNew:
                # Get the new header from the mapping dictionary
                newHeader = mappingDictOldNew[currentHeader]
                # Update the column header
                field.ColumnHeading = newHeader
                print(f"Changed header from {currentHeader} to {newHeader} in schedule '{viewSchedule.Name}'")
except Exception as e:
    TransactionManager.Instance.ForceCloseTransaction()
    OUT = "Error: " + str(e)
else:
    TransactionManager.Instance.TransactionTaskDone()

OUT = "Headers updated successfully."