Python Use Python to manipulate xmind files

Use Python to manipulate xmind files

by: Guest QQ : 1033553122

 

 

test environment

Win10

Python 3.5.4

XMind-1.2.0.tar.gz

download link:

https://files.pythonhosted.org/packages/7c/8c/e13a82fa9b0394c0d58248196d7d51d7274407cdebc1df36b76034ab990d/XMind-1.2.0.tar.gz

 

Create and update xmind files

#!/usr/bin/env python

# -*- coding:utf-8 -*-

 

import xmind

from xmind.core.const import TOPIC_DETACHED

from xmind.core.markerref import MarkerId

from xmind.core.topic import TopicElement

 

# Load has xmind file, and if not, then the new

workbook = xmind.load('D:\\example\\example.xmind')

 

= workbook.getPrimarySheet FIRST_SHEET ()  # get the first canvas

first_sheet.setTitle ( 'First Sheet' )   # Set the name of the canvas

= first_sheet.getRootTopic root_topic1 ()  # get the central theme of the canvas, creates a new blank canvas to create a central theme default

root_topic1.setTitle ( 'Example Topic' )   # set the theme name

 

 

 

= root_topic1.addSubTopic sub_topic1 () # create a sub-theme, and set the name

sub_topic1.setTitle("first sub topic")

 

sub_topic2 = root_topic1.addSubTopic()

sub_topic2.setTitle("second sub topic")

 

sub_topic3 = root_topic1.addSubTopic()

sub_topic3.setTitle("third sub topic")

 

#In addition to creating new sub-themes, you can also create free themes ( note : only the central theme supports the creation of free themes )

detached_topic1 = root_topic1.addSubTopic(topics_type=TOPIC_DETACHED)

detached_topic1.setTitle("detached topic")

detached_topic1.setPosition(0, 30)

 

# Create a sub-sub-theme theme

sub_topic1_1 = sub_topic1.addSubTopic ()

sub_topic1_1.setTitle("I'm a sub topic too")

 

= workbook.createSheet SECOND_SHEET, ()   # create a new canvas

second_sheet.setTitle('Second Sheet')

root_topic2 = second_sheet.getRootTopic()

root_topic2.setTitle('Root Node')

 

#Use other ways to create child theme elements

topic1 = TopicElement(ownerWorkbook=workbook)

topic1.setTopicHyperlink (first_sheet.getID ()) # create a link from the first theme of the canvas is a canvas

topic1.setTitle("redirection to the first sheet")

 

topic2 = TopicElement(ownerWorkbook=workbook)

topic2.setTitle("topic with an url hyperlink")

topic2.setURLHyperlink ( "https://www.cnblogs.com/shouke"# sub-themes set URL hyperlink

 

topic3 = TopicElement(ownerWorkbook=workbook)

topic3.setTitle("third node")

topic3.setPlainNotes ( "Notes for the this Topic"# Set Note to sub-theme (F4 in XMind)

topic3.setTitle("topic with \n notes")

 

topic4 = TopicElement(ownerWorkbook=workbook)

topic4.setFileHyperlink ( "d: \\ Example \ demo.jpg"# sub-themes hyperlink settings file

topic4.setTitle("topic with a file")

 

topic1_1 = TopicElement(ownerWorkbook=workbook)

topic1_1.setTitle("sub topic")

topic1_1.addLabel ( "A label"# Add tags for sub-theme ( Official XMind only CAN A One label )

# Add subtopics to non-central topic

topic1.addSubTopic(topic1_1)

 

topic1_1_1 = TopicElement(ownerWorkbook=workbook)

topic1_1_1.setTitle("topic can add multiple markers")

#Add tags to topics

topic1_1_1.addMarker (MarkerId.starBlue)

topic1_1_1.addMarker (MarkerId.flagGreen)

# Add the sub-theme of subtopics

topic1_1.addSubTopic(topic1_1_1)

 

topic2_1 = TopicElement(ownerWorkbook=workbook)

topic2_1.setTitle("topic can add multiple comments")

#Add comments to the topic

topic2_1.addComment("I'm a comment!")

topic2_1.addComment(content="Hello comment!", author='devin')

 

topic2.addSubTopic(topic2_1)

 

#Add child theme elements to the central theme

root_topic2.addSubTopic(topic1)

root_topic2.addSubTopic(topic2)

root_topic2.addSubTopic(topic3)

root_topic2.addSubTopic(topic4)

 

# Traversing subtopics

topics = root_topic2.getSubTopics()

for index, topic in enumerate(topics):

    topic.addMarker ( "priority-" + str (index + 1)) # to tag theme ( priority icon )

 

# Sub-theme 1 and sub-themes 2 to create a relationship

second_sheet.createRelationship(topic1.getID(), topic2.getID(), "relationship test")

 

# xmind.save (workbook) #Save and overwrite the original file

 

# Save only content.xml

# xmind.save (workbook = workbook, path = "d: \\ example \\ other.xmind", only_content = True) #Do not change the original file, save as another xmind file

 

# Save only content.xml , comments.xml , styles.xml

# xmind.save (workbook = workbook, path = "d: \\ example \\ other.xmind", except_revisions = True) #Do not change the original file, save as other xmind files

 

# Save everything, Revisions except to save space ( recommended )

# xmind.save (workbook = workbook, path = "d: \\ example \\ other.xmind", except_revisions = True) #Do not change the original file, save as other xmind files

 

# Save all the content, and save as other xmind files (Recommended)

xmind.save (Workbook = Workbook, path = 'D: \\ Example \\ other.xmind'# do not change the original file, save as other xmind file equivalents xmind.save (workbook, 'd: \\ example \\ exam.xmind ')

 

 

 

operation result

 

 

 

 

 

 

 

 

Parse xmind file

#!/usr/bin/env python

# -*- coding:utf-8 -*-

 

import json

import xmind

import pipes

 

def dict_to_prettify_json(data):

    print(json.dumps(data, indent=4, separators=(',', ': ')))

 

 

def custom_parse_xmind(workbook):

    elements = {}

 

    def _echo(tag, element, indent=0):

        title = element.getTitle()

        elements[element.getID()] = title

        print('\t' * indent, tag, ':', pipes.quote(title))

 

    def dump_sheet(sheet):

        root_topic = sheet.getRootTopic()

        _echo('RootTopic', root_topic, 1)

 

        for topic in root_topic.getSubTopics() or []:

            _echo('AttachedSubTopic', topic, 2)

 

        for topic in root_topic.getSubTopics(xmind.core.const.TOPIC_DETACHED) or []:

            _echo('DetachedSubtopic', topic, 2)

 

        for rel in sheet.getRelationships():

            id1, id2 = rel.getEnd1ID(), rel.getEnd2ID()

            print('Relationship: [%s] --> [%s]' % (elements.get(id1), elements.get(id2)))

 

    # Traversing the canvas

    for sheet in workbook.getSheets():

        _echo('Sheet', sheet)

        dump_sheet(sheet)

 

# Load has xmind file, and if not, then the new

workbook = xmind.load('D:\\example\\example.xmind')

Print (workbook.getData ()) # get the entire xmind data ( in the form of a dictionary )

dict_to_prettify_json(workbook.getData())

 

# Obtain data in a canvas ( in the form of a dictionary )

first_sheet = workbook.getPrimarySheet()

dict_to_prettify_json(first_sheet.getData())

 

# Obtain a topic data ( in the form of a dictionary )

root_topic = first_sheet.getRootTopic()

dict_to_prettify_json(root_topic.getData())

 

# Obtain data Comments

commentsbook = workbook.commentsbook

print(commentsbook.getData())

 

# Custom resolver

custom_parse_xmind(workbook)

 

 

Guess you like

Origin www.cnblogs.com/shouke/p/12685235.html