Setting trendline name in Chart fails with "Error saving workbook: 'str' object has no attribute 'to_tree"
This was an issue may be a bug.
Raised in Stackoverflow Openpyxl Post "how to use openpyxl modify trendline name".
In attempting to answer the question this what I found.
Tested with the latest release 3.1.2 and all Line Numbers quoted refer to that release.
When creating a Chart (Scatter in this case) and setting the name of a trendline for the Chart an attribute error is returned when the workbook is saved. The name for the trendline is '123'.
"Error saving workbook: 'str' object has no attribute 'to_tree'
Reason;
The error comes from the serialisable.py
to_tree
function @ line 106
When the 'tagname' is (str) 'trendline'
and the item being processed is (str) 'name' the following variables are set;
line 128: child_tag = (str) 'name'
line 130: obj = (str) '123'
At line 149 the child_tag 'name' is checked but fails because 'name' is not in self.__nested__
line 149: if child_tag in self.__nested__:
self.__nested__
contains other parameters that can be set like 'dispEq', 'dispRSqr', 'forward' etc but not name.
Code then drops to line 154
line 154: node = obj.to_tree(child_tag)
Given that 'obj' is a string for these values the line fails with the error;
AttributeError: 'str' object has no attribute 'to_tree'
As a test I edited trendline.py
to change 'name' to a NestedString and this did rectify the issue for the provided sample code.