我可以向 MS Project 中的时间刻度写入/提取数据吗?

问题描述 投票:0回答:1

我似乎无法弄清楚如何在任务使用情况视图中提取/写入时间刻度。 我似乎无法从这里提取数据,也无法写入数据。

我从员工那里获得了时间表数据,并且我不想手动输入 1000 多个任务的工时。

我花了大约 2-3 天的时间试图弄清楚如何在 Microsoft Project 中提取/添加数据到该字段,但似乎无法解决。

我尝试寻找文档寻求帮助,并且查看了 VBA 代码,我觉得我的代码是正确的,但我仍然无法让它工作。 时间刻度

    import pythoncom
    import win32com.client

    # Initialize COM connection
    pythoncom.CoInitialize()

    # Create an instance of the Microsoft Project application
    ms_project = win32com.client.Dispatch('MSProject.Application')

    # Make the application visible (optional)
    ms_project.Visible = True

    # Open a project file
    ms_project.FileOpen('D:\\Manufacturing Resourcing\\Test Folder\\Test.mpp')
    project = ms_project.ActiveProject

    # Constants for TimeScaleData
    pjAssignmentTimescaledWork = 35  
    pjTimescaleDays = 4            

    # Function to print task timephased data
    def print_task_timephased_data(task):
        print(f"Task: {task.Name}")
        if task.Assignments.Count > 0:
            for assignment in task.Assignments:
                print(f"  Assignment: {assignment.ResourceName}")
                try:
                # Get timescale data for each assignment
                    timephased_data = assignment.TimescaleData(task.Start, task.Finish, pjTimescaleDays,         pjAssignmentTimescaledWork)
                    for data in timephased_data:
                        print(f"    Start Date: {data.StartDate}, Work: {data.Value}")
                except Exception as e:
                    print(f"Error retrieving timephased data: {e}")

    # Iterate through all tasks and print their timephased data
    for task in project.Tasks:
        if task is not None and task.Start and task.Finish:  # Check to ensure the task is valid and has     start/finish dates
            print_task_timephased_data(task)

    pythoncom.CoUninitialize()
python extract project project-management
1个回答
0
投票

我能够使用下面的示例块写入任务使用情况中的时间线视图。这可以扩展到使用更大的数据集等。

import win32com.client
from datetime import datetime

def set_specific_timephased_data():
    try:
        ms_project = win32com.client.Dispatch("MSProject.Application")
        ms_project.Visible = True
    except Exception as e:
        print(f"Error initializing MS Project: {e}")
        return
    try:
        project = ms_project.ActiveProject
    except AttributeError:
        print("No active project found.")
        return

    # Get the first task and its first assignment for demonstration purposes
    if project.Tasks.Count > 0:
        task = project.Tasks.Item(1)
        if task.Assignments.Count > 0:
            assignment = task.Assignments.Item(1)
            
            # Define the start and end dates for the timephased data
            start_date = project.ProjectStart
            end_date = project.ProjectFinish

            # Access the timephased data for work in days
            timescale_data = assignment.TimeScaleData(start_date, end_date, win32com.client.constants.pjAssignmentTimescaledWork, win32com.client.constants.pjTimescaleDays)
            
            # Dictionary of specific dates and corresponding work values (in minutes)
            specific_work_values = {
                datetime(2024, 6, 1): 2 * 60,
                datetime(2024, 6, 2): 1 * 60,
                datetime(2024, 6, 3): 3 * 60,
            }
            
            # Set specific values for specific dates
            for time_value in timescale_data:
                start_date_time = datetime.strptime(time_value.StartDate.strftime('%Y-%m-%d'), '%Y-%m-%d')
                end_date_time = datetime.strptime(time_value.EndDate.strftime('%Y-%m-%d'), '%Y-%m-%d')
                
                print(f"    TimeScaleValue Object: {time_value}")
                print(f"    Current Work from {start_date_time} to {end_date_time}: {time_value.Value} minutes")
                if start_date_time in specific_work_values:
                    print(f"    Modifying object: {time_value} on {start_date_time}")
                    time_value.Value = specific_work_values[start_date_time]
                    print(f"    Updated Work on {start_date_time} to {time_value.Value} minutes")

            print("Timephased data updated successfully.")

set_specific_timephased_data()
© www.soinside.com 2019 - 2024. All rights reserved.