首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用python通过MPXJ库读取MS项目中的所有行和列

使用python通过MPXJ库读取MS项目中的所有行和列
EN

Stack Overflow用户
提问于 2022-03-16 11:11:52
回答 1查看 420关注 0票数 0

因此,我试图使用MPXJ库在python中读取.mpp文件,我能够获得任务名称、相应的任务开始/结束日期等。我也希望得到这些数据的相应类型。此外,我还想读取所有列及其各自的类型,包括它们的依赖关系附件和任何其他自定义字段。我在他们的文档中找不到这一点,我应该使用哪个函数,所以在这里问

全码

代码语言:javascript
运行
复制
import jpype
import mpxj

jpype.startJVM()
from net.sf.mpxj.reader import UniversalProjectReader
project = UniversalProjectReader().read('example.mpp')

print("Tasks")
tasks = project.getTasks()

for task in tasks:
    print(task.getID().toString() + "\t" + task.getName() + "\t" + task.getStart() + "\t" + task.getFinish() + "\t" + task.getDuration())

无法工作的自定义字段代码

代码语言:javascript
运行
复制
customFields = project.getCustomFields()
print(customFields)
EN

Stack Overflow用户

发布于 2022-04-29 17:07:00

不幸的是,我在Python中还没有很多用于MPXJ的例子,但是我正在研究它!同时,我已经修改了您的代码,以演示如何检索项目中所有自定义字段的详细信息,然后只将这些字段筛选到任务字段,最后使用字段类型动态检索自定义字段的值,并将“别名”用作列标题:

代码语言:javascript
运行
复制
import jpype
import mpxj

jpype.startJVM()

from net.sf.mpxj.reader import UniversalProjectReader
from net.sf.mpxj import FieldTypeClass
from net.sf.mpxj import TaskField

project = UniversalProjectReader().read(
    'project-with-custom-fields.mpp')

# Just to get started, let's see what tasks we have
print("Tasks")
tasks = project.getTasks()

for task in tasks:
    print(task.getID().toString() + "\t" +
          task.getName())
print()

# OK, so what custom field so we have?
print("Custom Fields")
for field in project.getCustomFields():
    print(field.getFieldType().getFieldTypeClass().toString() + "\t" +
          field.getFieldType().toString() + "\t" +
          field.getAlias())
print()

# Ah! We have custom field definitions here for different entity types
# (tasks, resources etc). Let's filter that list down to just task custom
# fields and print those.
task_custom_fields = list(filter(lambda field: field.getFieldType(
).getFieldTypeClass() == FieldTypeClass.TASK, project.getCustomFields()))

print("Task Custom Fields")
for field in task_custom_fields:
    print(field.getFieldType().getFieldTypeClass().toString() + "\t" +
          field.getFieldType().toString() + "\t" + field.getAlias())

# Let's build a report showing the ID, Name and any custom fields for each task.
# First we'll build a list of column headings and a list of field types
column_names = ['ID', 'Name']
column_types = [TaskField.ID, TaskField.NAME]
for field in task_custom_fields:
    column_names.append(str(field.getAlias()))
    column_types.append(field.getFieldType())

# Now we can print the column headings, then iterate through the tasks
# and retrieve the values using the field types.
print('\t'.join(column_names))
for task in tasks:
    column_values = map(lambda type: str(
        task.getCachedValue(type)), column_types)
    print(task.getID().toString() + "\t" +
          task.getName() + "\t" +
          '\t'.join(column_values))

jpype.shutdownJVM()

由于Python是底层Java版本的包装器,通过咨询Javadoc http://www.mpxj.org/apidocs/index.html,您将发现许多有关类和方法的有用细节。

票数 1
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71496071

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档