Create MS Project in Python | MS Project API Python

Project management is essential for any organization that wants to thrive in a competitive world. Microsoft Project is popular project management software that manages projects easily and flexibly. In some cases, you may need to create and manage MS Projects programmatically without using the desktop application. This post shows how to create an MS Project programmatically using Python, unlocking new levels of automation and productivity.

This article covers the following topics:

  1. Python API to Create and Manage MS Projects
  2. Create MS Project from Scratch using Python
  3. Add Tasks to the Project
  4. Add Resources to the Project
  5. Assign Tasks to Project Resources
  6. Set MS Project’s Calculation Mode
  7. Convert MPP to XML

Python API to Create and Manage MS Projects

We will use Apose.Tasks for Python to create our MS Project. It is a powerful API that lets developers work with Microsoft Project files (MPP) programmatically. It provides features to read, write, manipulate, and manage Project files without needing Microsoft Project software.

Please download the package or install the API from PyPI using the following pip command in the console:

pip install aspose-tasks

Create MS Project File in Python

MS Project uses MPP as the proprietary file format. We can create the first MS Project from scratch by following these steps:

  • Create an instance of the Project class.
  • Call the save() method with the output MPP file path and SaveFileFormat as arguments.

The code sample below shows how to create MS Project files using Python.

Create MS Project File in Python

Add Tasks to MS Project in Python

We can add simple tasks, summary tasks, or subtasks programmatically by following these steps:

  1. Create an instance of the Project class.
  2. Add a new summary task to the project’s Tasks collection using the root_task.children.add(string) method.
  3. Add a subtask to the summary task using the task.children.add(string) method.
  4. Call the save() method to save the project.

The code sample below shows how to create and add tasks to MS Project files using Python.

Add Tasks to MS Project File in Python

Add Resources to MS Project using Python

Resources are the entities that perform tasks in a project. Add resources programmatically by following these steps:

  • Create or load an existing project using the Project class.
  • Add a new resource to the project’s Resources collection using the project.resources.add(string) method.
  • Call the save() method to save the project.

The code sample below shows how to add resources to an MS Project file using Python.

Assign Tasks to Resources in a Project using Python

After adding tasks and resources, assign tasks to resources with these steps:

  • Create an instance of the Project class.
  • Add a task to the project’s Tasks collection.
  • Add a resource to the project’s Resources collection.
  • Assign the task to the resource using the project.resource_assignments.add(task, resource) method.
  • Call the save() method with the output MPP file path and SaveFileFormat as arguments.

The code sample below shows how to create and assign tasks to resources in an MS Project using Python.

Assign Tasks to Resource in a Project using Python

Set MS Project Calculation Mode using Python

The project’s calculation mode defines when changes are calculated. Set the calculation mode by following these steps:

  • Create or load a project using the Project class.
  • Set the project’s calculation_mode property using the CalculationMode enumeration value.
  • Save the project.

The code sample below shows how to set MS Project’s calculation mode using Python.

Convert MPP to XML

We can save MS Project in formats other than MPP, such as XML, as shown below:

Please check out the detailed tutorial on how to convert MPP to XML in Python.

Try Aspose.Tasks for Free

You can get a free temporary license to try Aspose.Tasks for Python without evaluation limitations.

MS Project API Python

Explore these resources to learn more about creating and managing MS Projects with Aspose.Tasks for Python:

Conclusion

In this post, we learned how to create an MS Project file in Python. The step‑by‑step guide and code samples show how easy it is to create projects, add tasks and resources, and assign tasks using the Aspose.Tasks for Python library. Whether you are a beginner or an experienced developer, Aspose.Tasks for Python simplifies project creation and manipulation without Microsoft Project. Take advantage of this library to improve your project management and automation. For any questions, please contact us on our free support forum.

See Also