mmap文件转换为mm文件

     之前一直使用MindManager编辑思维导图,最近开始尝试使用FreeMind,发现这个更小巧轻便,但是FreeMind无法打开MindManager编辑保存的mmap文件,在网上搜了下关于mmap文件转换为mm文件的资料,参考网上方法并做了点小修改。      

      原方法的思路是利用python的zipeFile打开mmap文件,然后读取里面的“Document.xml”文件,再将mm文件的格式应用到“Document.xml”文件,最后保存为mm文件。

     思路挺简单,但是使用过程中出现几个小问题,具体现象和解决方法如下:

     1. 缺少xml解析库:import libxml2 ImportError: No module named libxml2。

         解决方法:在python非官方扩展库下载对应的libxml2安装包。

     2. mmap文件打开失败:zipfile.BadZipfile: File is not a zip file

         解决方法:利用解压缩工具直接提取出“Document.xml”文件用于内容读取。

     更改后代码如下,可以复制后保存为mm2mf.py

     使用方法:python mm2mf.py Document.xml

#!/usr/bin/python
#
# mm2fm - Mind Manager to FreeMind file converter
#
# Copyright (C) 2007  David Symons <david.symons@liberatedcomputing.net>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#
# Dependencies: python, python-libxml2, python-libxslt1
#
# Instructions: Make the file executable and put it somewhere in your PATH.
#               Also see 'mm2fm --help'.

import libxml2
import libxslt
import os.path
import sys
import zipfile

version = '0.1'
usage = """
Usage: mm2fm file [file ...]

For more details see 'mm2fm --help'
"""

if len(sys.argv) < 2:
  print usage
  sys.exit()

elif sys.argv[1] == "--help":
  print usage
  print """Converts a file or group of files to Freemind format.  Creates new files using the existing filename but with a .mm extension.

Examples: mm2fm MyMap.mmap
          mm2fm MyMap1.mmap MyMap2.mmap
          mm2fm *.mmap
"""
  sys.exit()

elif sys.argv[1] == "--version":
  print """
Version: %s
""" % version
  sys.exit()

# The XSLT stylesheet.  It was added to the Freemind wiki[1] by unnamed
# contributor.  Many thanks and kudos to him/her as this really is the magic in
# this program.
#
# [1] http://freemind.sourceforge.net/wiki/index.php/Import_and_export_to_other_applications
#
stylesheet = """<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:ap="http://schemas.mindjet.com/MindManager/Application/2003"
>
      <xsl:output
        method="xml"
      version="1.0"
      encoding="iso-8859-1"
      omit-xml-declaration="yes"
      indent="yes"
      />     
  <xsl:template match="/">
    <xsl:element name="map">
      <xsl:attribute name="version">0.7.1</xsl:attribute>
      <xsl:apply-templates select="ap:Map/ap:OneTopic/ap:Topic" />
    </xsl:element>
  </xsl:template>
  <xsl:template match="ap:Topic">
    <xsl:element name="node">
      <xsl:attribute name="TEXT">
        <xsl:value-of select="ap:Text/@PlainText" />
      </xsl:attribute>        <xsl:if test="ap:Text/ap:Font/@Color">
        <xsl:attribute name="COLOR">
          <xsl:value-of select="concat('#', substring(ap:Text/ap:Font/@Color, 3, 6))" />
        </xsl:attribute>
      </xsl:if>
      <xsl:variable name="OId" select="@OId" />
      <xsl:variable name="relation" select="/ap:Map/ap:Relationships/ap:Relationship[ap:ConnectionGroup[@Index=0]/ap:Connection/ap:ObjectReference/@OIdRef=$OId]" />
      <xsl:if test="$relation">
        <xsl:variable name="toId" select="$relation/ap:ConnectionGroup[@Index=1]/ap:Connection/ap:ObjectReference/@OIdRef" />
        <xsl:element name="arrowlink">
          <xsl:attribute name="ENDARROW">Default</xsl:attribute>
          <xsl:attribute name="DESTINATION">
            <xsl:value-of select="$relation/ap:ConnectionGroup[@Index=1]/ap:Connection/ap:ObjectReference/@OIdRef" />
          </xsl:attribute>
          <xsl:attribute name="STARTARROW">None</xsl:attribute>
        </xsl:element>
      </xsl:if>
      <xsl:variable name="toId" select="/ap:Map/ap:Relationships/ap:Relationship/ap:ConnectionGroup[@Index=1]/ap:Connection/ap:ObjectReference[@OIdRef=$OId]/@OIdRef" />
      <xsl:if test="$toId">
        <xsl:attribute name="ID">
          <xsl:value-of select="$toId" />
        </xsl:attribute>
      </xsl:if>
      <xsl:apply-templates select="ap:SubTopics"/>
    </xsl:element>
  </xsl:template>
</xsl:stylesheet>
"""

for filename in sys.argv[1:]:
  styledoc = libxml2.parseDoc(stylesheet)
  style = libxslt.parseStylesheetDoc(styledoc)
  #f = open( filename, 'r' )
  #z = zipfile.ZipFile( f )
  #content = z.read( "Document.xml" )
  f = open(filename, 'r')
  content = f.read()
  f.close()
  doc = libxml2.parseDoc( content )
  result = style.applyStylesheet(doc, None)
  str = style.saveResultToString(result)
  style.freeStylesheet()
  doc.freeDoc()
  result.freeDoc()
  outfile = open( os.path.splitext( filename )[0] + '.mm', 'w' )
  outfile.write( str )
  outfile.close()

  • 1
    点赞
  • 0
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

©️2021 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值