Skip to main content

Rename a column from within Python, error "Property 'NewField.Type' can not be set."

Suppose I have a simple task, I want to rename a column. My IDEAScript is as follows:
 
Sub Main
    Call ModifyField()
End Sub
 
Function ModifyField
    Set db = Client.OpenDatabase("myfile.IMD")
    Set task = db.TableManagement
    Set field = db.TableDef.NewField
    field.Name = "newname"
    field.Description = "field"
    field.Type = WI_CHAR_FIELD
    field.Equation = ""
    field.Length = 1
    task.ReplaceField "oldname", field
    task.PerformTask
    Set task = Nothing
    Set db = Nothing
    Set field = Nothing
End Function
 
Now, I want to perform the same task using Python win32com. I tried:
 
import win32com.client as win32ComClient
wd = os.getcwd()
try:
    idea = win32ComClient.Dispatch(dispatch="Idea.IdeaClient")
    db = idea.OpenDatabase(os.path.join(wd, 'myfile.IMD'))
    task = db.TableManagement()
    field = db.TableDef().NewField()
    field.Name = "oldname"
    field.Description = "description"
    field.Type = WI_CHAR_FIELD
    field.Equation = ""
    field.Length = 1
    task.ReplaceField("newname", field)
    task.PerformTask
    task = Nothing
    db = Nothing
    field = Nothing
    idea = None
finally:
    idea = None
 
 
I first had some errors on the db.TableManagement() and the db.TableDef().NewField() statement, as I missed the brackets. I also had to change task.ReplaceField "oldname", field to task.ReplaceField("newname", field). This seemed to be fixed, however now I get the error:
AttributeError: Property 'NewField.Type' can not be set.and it marks the line field.Type = WI_CHAR_FIELD. I tried different combinations, like field.Type = "WI_CHAR_FIELD" or field.Type(WI_CHAR_FIELD) but it does not work. So how can I get this working? Thanks for the help!

Brian Element Wed, 06/07/2023 - 12:33

The WI_CHAR_FIELD is a constant but within IDEAScript it knows the value.  When you port it over to python, python has no idea what the value of that constant is so you will need to define it.  Here are some of them.

 

WI_VIRT_CHAR = 0
WI_VIRT_NUM = 1
WI_VIRT_DATE = 2
WI_CHAR_FIELD = 3
WI_NUM_FIELD = 4
WI_DATE_FIELD = 5
WI_EDIT_NUM = 6
WI_EDIT_CHAR = 7
WI_EDIT_DATE = 8
WI_MULTISTATE = 9
WI_BOOL = 10
WI_TIME_FIELD = 11
WI_EDIT_TIME = 12
WI_VIRT_TIME = 13

Bert_B Thu, 06/08/2023 - 04:18

Dear Brian,
 
once again a big thanks for the fast and good answer!
 
I now changed it (I put a WI_CHAR_FIELD = 3 statement before the try:) and get no error anymore, however, nothing is changed. Python works and also the imd file is opened in IDEA, but the column does not get renamed?
 
I closed the file in IDEA before running the code and also refreshed in IDEA, but it just displays the old column name, no change.

Brian Element Thu, 06/08/2023 - 07:09

Hi Bert, here is an update for you to try:


import win32com.client as win32ComClient

try:
    idea = win32ComClient.Dispatch(dispatch="Idea.IdeaClient")
    db = idea.OpenDatabase('myfile.IMD')
    task = db.TableManagement()
    field = db.TableDef().NewField()
    field.Name = "NEW_NAME"
    field.Description = "description"
    field.Type = WI_CHAR_FIELD
    field.Equation = ""
    field.Length = 1
    task.ReplaceField("OLD_NAME", field)
    task.PerformTask()
finally:
    task = None
    db = None
    field = None
    idea = None

Brian Element Thu, 06/08/2023 - 07:14

Some problems I found, I removed the path info as when you open IDEA it will automatically be in the current project folder, so you don't really need it or you should probably be using the internal IDEA working folder line.  What you had was fine but just not needed in this scenario.

I capitalized the field names.  In IDEA the field names are always capitalized and if you give it to them lower case it will capitalize it for you but not always the way you might expect, just best practice to captalize them yourself instead of having IDEA do it for you.

You were missing the () from the PerformTask, that is probably why it wouldn't run.

The Nothing should be changed to None in Python.

I moved all the = None to the finally as this will always run even if the try fails, so you want to make sure anything you open has been properly closed.

Bert_B Thu, 06/08/2023 - 10:06

Dear Brian,
 
once a big thanks for the help! Especially the additional remarks. And yes, I missed the brackets () again at PerformTask. It works now.