因此,我试图使用MPXJ库在python中读取.mpp文件,我能够获得任务名称、相应的任务开始/结束日期等。我也希望得到这些数据的相应类型。此外,我还想读取所有列及其各自的类型,包括它们的依赖关系附件和任何其他自定义字段。我在他们的文档中找不到这一点,我应该使用哪个函数,所以在这里问
全码
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())
无法工作的自定义字段代码
customFields = project.getCustomFields()
print(customFields)
发布于 2022-04-29 17:07:00
不幸的是,我在Python中还没有很多用于MPXJ的例子,但是我正在研究它!同时,我已经修改了您的代码,以演示如何检索项目中所有自定义字段的详细信息,然后只将这些字段筛选到任务字段,最后使用字段类型动态检索自定义字段的值,并将“别名”用作列标题:
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,您将发现许多有关类和方法的有用细节。
https://stackoverflow.com/questions/71496071
复制相似问题