如何使用 Python 使用 MS Project TimeScaleValue 对象

How to work with MS Project TimeScaleValue object using Python

我正在尝试使用 win32com.client 从 MPP 文件中读取数据来创建图表。但是参考了Microsoft's documentation,我尝试从简单的代码入手

import win32com.client

mppFileName = 'C:/python/test.mpp'

mpp = win32com.client.Dispatch("MSProject.Application")
mpp.visible = 0
    
mpp.FileOpen(mppFileName)
project = mpp.ActiveProject

ResourceList = project.Resources
assignments = project.Assignments
    
for resource in ResourceList:
    if resource.Work != 0:
        print(resource.Name, ' ', resource.Job)
     
mpp.FileClose(Save=0)
  
exit()

但是此代码在 assignments= 行上产生错误(Assignments 是 'unknown')。有谁知道使用 Assignments 和 TimeScaleValues 对象的正确方法?我的目标是创建一个图表,其中包含按周分配给每个项目资源的工作量。

您需要循环遍历 Assignments collection for each Resource. Then for each Assignment, loop through the time periods you want (e.g. weeks) using the TimeScaleData method which is the key to reading (and setting) work by day/week/etc. This method returns a TimeScaleValues collection of TimeScaleValue objects。

伪代码:

for r in ResourceList:
  asgmts = r.Assignments
  for a in asgmts 
    ''' create timescalevalues collection to loop through time periods'''
    tsvs = a.TimeScaleData(start, end, type, unit)
    for tsv in tsvs
      ' aggregate the work as needed

其他有用的 object 知识:Application, Tasks collection, Task object, and Resources collection

也在 Whosebug 中搜索 similar questions such as TimeScaleData in Project using .net。大多数问题和答案不会 python (c#, vb.net, vba) 但使用项目 object 模型的核心将适用。