SSブログ

WinMerge で OpenOffice.org Writer の .odt ファイルを扱ってみる。 [OpenOffice.org]

二つのファイルの差分を調べる時、昔は Visual Studio 同梱の WinDiff を使っていたけど、いつの頃からか WinMerge を使うようになった。 Merge って名前がついてるけど、Visual Studio 同梱の WinDiff のようにタイリング表示で検出した差分を表示してくれるし、マージもできる。 ついでにそのタイリング表示している画面から内容を直接変更することもできるので、手作業でマージw する時に便利。 もちろんフォルダ指定によってサブフォルダ内の複数ファイルに対しても処理可能。

プラグイン機構を持っていて、プラグインをインストールすれば MS-Office の Wordファイルや Excel ファイルなどの非テキストファイルの差分も参照できるというありがたいソフトでして。

WinMerge日本語版の Webページにて、MS-Word や MS-Excel のプラグインを公開している Webページが紹介されている。 でも、どちらも MS-Word と MS-Excel を COM経由で利用しているので MS-Word と MS-Excel それ自身が必要だったり。 ただ、MS-Word や MS-Excel を持っていないならば、xdoc2txt という単独で動作するファイル変換プログラムを利用した xdocdiff WinMerge Plugin があるので、そちらをインストールすれば OK。 xdoc2txt だと MS-Word や MS-Excel だけでなく PowerPoint や PDF、一太郎ファイルも対応しているらしいので便利だと思う。 (何がすごいって Lotus 1-2-3 や松、OASYS にまで対応しているという・・・)

で、xdoc2txt は OpenOffice.org のファイルにはまだ対応していないみたいなので、さっきの続きとして WinMerge の OpenOffice.org Writer ファイルプラグインなんかを書いてみたりする。 OdtToText.sct 辺りのファイル名で WinMerge の MergePlugins フォルダへ保存するだけ。 WinMerge のプラグインメニューで自動展開にしておく必要が・・・あるか?

書式やレイアウトを考慮せずにテキストを抜き出しただけの内容を比較しているので、文書内に挿入した画像を差し替えただけとか左寄せを右寄せにしただけとかいう場合には差異無しになるような気がするので実用性は極めて低い・・ね。

その辺も考えて処理すればもうちょっと良くなるだろうけど、OpenOffice.org は普段使わないからとりあえずここまでw 個人的には、市場や技術がより良い形で発展するために、OpenOffice.org には Microsoft Office のカウンターとして頑張ってもらえれば・・・という感じなので。

もっとも、OpenOffice.org 自身にドキュメントを比較する機能があるし、まるで校正した文書のごとく表示してくれるので、そっちを使うのが確実な訳でw ただ、複数ファイル群を一気に比較してチェックするというのは出来ないので、その時は WinMerge + プラグインが便利かもしれない。

<scriptlet>
    <implements type="Automation" id="dispatcher">
        <property name="PluginEvent"><get/></property>
        <property name="PluginDescription"><get/></property>
        <property name="PluginFileFilters"><get/></property>
        <property name="PluginIsAutomatic"><get/></property>
        <method   name="UnpackFile"/>
        <method   name="PackFile"/>
    </implements>
    <script language="VBS">

Option Explicit

Function get_PluginEvent()
    get_PluginEvent = "FILE_PACK_UNPACK"
End Function

Function get_PluginDescription()
    get_PluginDescription = "OdtToText"
End Function

Function get_PluginFileFilters()
    get_PluginFileFilters = "\.odt$"
End Function

Function get_PluginIsAutomatic()
    get_PluginIsAutomatic = True
End Function

Function UnpackFile(fileSrc, fileDst, pbChanged, pSubcode)
    Dim oOOoServiceManager
    Set oOOoServiceManager _ 
      = CreateObject("com.sun.star.ServiceManager")
    Dim oOOoDesktop
    Set oOOoDesktop _ 
      = oOOoServiceManager.createInstance("com.sun.star.frame.Desktop")

    Dim oPropertyValue
    Set oPropertyValue _ 
      = oOOoServiceManager.Bridge_GetStruct("com.sun.star.beans.PropertyValue")
    oPropertyValue.Name = "Hidden"
    oPropertyValue.Value = True

    Dim oODT
    Set oODT = oOOoDesktop.LoadComponentFromURL(_ 
      "file:///" & Replace(fileSrc, "\", "/"), _ 
      "_blank", 0, Array(oPropertyValue))
    Set oPropertyValue = Nothing

    Dim oEnum
    Set oEnum = oODT.Text.createEnumeration

    Dim oTextElement
    Dim sBody
    sBody = ""
    While oEnum.hasMoreElements
        Set oTextElement = oEnum.nextElement
        If oTextElement.supportsService("com.sun.star.text.TextTable") _ 
        Or oTextElement.supportsService("com.sun.star.text.Paragraph") Then
            sBody = sBody & oTextElement.String & VbCrLf
        End If
        Set oTextElement = Nothing
    Wend

    Dim oFs
    Set oFs = CreateObject("Scripting.FileSystemObject")
    Dim oFile
    Set oFile = oFs.CreateTextFile(fileDst, True)
    oFile.Write sBody
    oFile.Close
    Set oFile = Nothing
    Set oFs = Nothing

    Set oTextElement = Nothing
    Set oEnum = Nothing
    oODT.Dispose
    Set oODT = Nothing
    Dim oDocs
    Set oDocs = oOOoDesktop.getComponents().createEnumeration()
    If Not oDocs.hasMoreElements() Then
        oOOoDesktop.Terminate
    End If
    Set oDocs = Nothing
    Set oOOoDesktop = Nothing
    Set oOOoServiceManager = Nothing

    pbChanged = True
    pSubcode = 0

    UnpackFile = True
End Function

Function PackFile(fileSrc, fileDst, pbChanged, pSubcode)
    PackFile = False
End Function

    </script>
</scriptlet>

(2006.8.23追記)あかん。 それじゃアカン。 orz
それじゃ、表が挿入されてるとエラーが発生しちゃう。

$ diff -u OdtToText.sct.old  OdtToText.sct
--- OdtToText.sct.old   2006-08-21 21:06:56.990750000 +0900
+++ OdtToText.sct       2006-08-23 20:02:56.009375000 +0900
@@ -73,13 +73,22 @@
     Set oEnum = oODT.Text.createEnumeration

     Dim oTextElement
+    Dim sCellNames
+    Dim oCell
+    Dim i
     Dim sBody
     sBody = ""
     While oEnum.hasMoreElements
         Set oTextElement = oEnum.nextElement
-        If oTextElement.supportsService("com.sun.star.text.TextTable") _
-        Or oTextElement.supportsService("com.sun.star.text.Paragraph") Then
+        If oTextElement.supportsService("com.sun.star.text.Paragraph") Then
             sBody = sBody & oTextElement.String & VbCrLf
+        ElseIf oTextElement.supportsService("com.sun.star.text.TextTable") Then
+            sCellNames = oTextElement.getCellNames
+            For i = 0 To UBound(sCellNames)
+                Set oCell = oTextElement.getCellByName(sCellNames(i))
+                sBody = sBody & oCell.String & VbCrLf
+                Set oCell = Nothing
+            Next
         End If
         Set oTextElement = Nothing
     Wend

$

追記:表の処理をちょっと変えてみた。


nice!(0)  コメント(0)  トラックバック(0) 
共通テーマ:パソコン・インターネット

nice! 0

コメント 0

コメントを書く

お名前:
URL:
コメント:
画像認証:
下の画像に表示されている文字を入力してください。

トラックバック 0

この広告は前回の更新から一定期間経過したブログに表示されています。更新すると自動で解除されます。