Search code examples
pythonwin32comms-project

<unkown>.COLUMN_NAME while accessing ms project column using python


I was successful in accessing most of the columns from MS Project File, but there's a column named 'Baseline Start'. Whenever I try to access it using python it throws me the following exception:

enter image description here

Here's the code I am using:

import win32com.client
import pandas as pd

class ToExcel():
    def __init__(self):                
        file_project = 'PATH\\TO\\PROJECT\\FILE.mpp'                                        
        try:
            project_app = win32com.client.Dispatch('MSProject.Application')
            project_app.Visible = 1
            try:
                columnNames = ['Baseline Start']
                taskData = []                                

                project_app.FileOpen(file_project)
                project = project_app.ActiveProject
                project_tasks = project.Tasks                                                                
                
                for task in project_tasks:                                        
                    baseline_col = task.Baseline                                      

                    taskData.append([baseline_col])

                df = pd.DataFrame(taskData , columns=columnNames)                              
                print(df)
            except Exception as e:
                print('Error :',e)                
        
            #project_app.Quit()
        except Exception as e:
            print('Error :',e)            


if __name__ == '__main__':
    inst = ToExcel()

Note that I am just using 'Baseline' in 'task.Baseline', thats because I successfully accessed values of column named 'Task Name' using 'task.Task'.


Solution

  • The exception is thrown because there is no field called 'Baseline'. Instead there are dozens of baseline fields such as Baseline Start, Baseline Finish, Baseline Duration, etc.

    Use baseline_col = task.BaselineStart instead.

    See Available fields reference for more information.