[UDF] OpenDocumentFormat, ODT, OpenOffice, LibreOffice

    • Offizieller Beitrag

    Hallo,

    Der Anfang einer UDF für das ODT-Format, das LibreOffice/OpenOffice nutzen. Angenehmerweise ist das Format nicht nur dokumentiert, sondern auch auf den Schultern von Giganten gebaut: XML für die Daten und Styles, beide möglichst sauber getrennt, alle relevanten Daten dann gemeinsam in ein Zip-Archiv gepackt.
    Meine UDF nutzt 7-zip in der Kommandozeilenversion, weil alle Zip-UDFs, die mir untergekommen sind, nicht ordentlich funktioniert haben.

    Funktionen:

    Code
    Func _ODTUnpack($sFile, $sTarget = "default")
    Func _ODTGetContent($sTarget)
    Func _ODTSetContent($sHandle, $sContent)
    Func _ODTGetStyles($_handle)
    Func _ODTSetStyles($_handle, $_styles)
    Func _ODTRepack($sTarget, $sBasefile, $sNewfile = "same")

    Die UDF im Ganzen:

    Spoiler anzeigen
    [autoit]

    #cs ----------------------------------------------------------------------------

    AutoIt Version: 3.3.6.1
    Author: Johannes Mitlmeier

    Script Function:
    OpenDocument (LibreOffice/OpenOffice) function library.
    Version 0.1
    #ce ----------------------------------------------------------------------------

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    #include-once
    #include <File.au3>

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    ; #FUNCTION# ====================================================================================================
    ; Name...........: _ODTUnpack
    ; Description....: Unpacks a odt file (which is essentially zipped) into the given folder
    ; Syntax.........: _ODTUnpack($sFile, $sTarget)
    ; Parameters.....: $sFile - Full filename of the odt file you want to unpack
    ; $sTarget - [Optional] Name of the directory to unpack to (default: in Tempdir)
    ;
    ; Return values..: Success - returns name of the directory the data have been unpacked to
    ; Failure - returns 0 and sets @error to 1
    ; Author.........: Johannes Mitlmeier
    ; Modified.......: 2011-03-25
    ; Remarks........:
    ; Related........:
    ; Link...........:
    ; Example........:
    ; ===============================================================================================================
    Func _ODTUnpack($sFile, $sTarget = "default")
    If $sTarget == "default" Then $sTarget = @TempDir & "\odt"
    DirRemove($sTarget, 1)
    RunWait('"' & @ScriptDir & '\7za.exe" x "' & $sFile & '" "-o' & $sTarget & '" -y')
    If FileExists($sTarget) Then Return $sTarget
    SetError(1, 0, 0)
    EndFunc ;==>_ODTUnpack

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    ; #FUNCTION# ====================================================================================================
    ; Name...........: _ODTGetContent
    ; Description....: returns content of the content.xml file inside the given directory
    ; Syntax.........: _ODTGetContent($sTarget)
    ; Parameters.....: $sTarget - name of the directory to search in (return value of _ODTUnpack)
    ;
    ; Return values..: Success - returns content.xml as a string
    ; Failure - returns return value of FileRead
    ; Author.........: Johannes Mitlmeier
    ; Modified.......: 2011-03-25
    ; Remarks........:
    ; Related........:
    ; Link...........:
    ; Example........:
    ; ===============================================================================================================
    Func _ODTGetContent($sTarget)
    Return FileRead($sTarget & "\content.xml")
    EndFunc ;==>_ODTGetContent

    [/autoit] [autoit][/autoit] [autoit]

    ; #FUNCTION# ====================================================================================================
    ; Name...........: _ODTSetContent
    ; Description....: Sets the content of the content.xml file inside the given directory
    ; Syntax.........: _ODTSetContent($sTarget, $sContent)
    ; Parameters.....: $sTarget - name of the directory to search in (return value of _ODTUnpack)
    ; $sContent - new content of the file, previous content will be erased
    ;
    ; Return values..: Success - returns 0
    ; Failure - returns -1 and sets @error as failed FileOpen
    ; Author.........: Johannes Mitlmeier
    ; Modified.......: 2011-03-25
    ; Remarks........:
    ; Related........:
    ; Link...........:
    ; Example........:
    ; ===============================================================================================================
    Func _ODTSetContent($sHandle, $sContent)
    Return _ODTWriteToFile($sHandle, "content.xml", $sContent)
    EndFunc ;==>_ODTSetContent

    [/autoit] [autoit][/autoit] [autoit]

    Func _ODTGetStyles($_handle)
    Return FileRead($_handle & "\styles.xml")
    EndFunc ;==>_ODTGetStyles

    [/autoit] [autoit][/autoit] [autoit]

    Func _ODTSetStyles($_handle, $_styles)
    EndFunc ;==>_ODTSetStyles

    [/autoit] [autoit][/autoit] [autoit]

    ; #FUNCTION# ====================================================================================================
    ; Name...........: _ODTRepack
    ; Description....: Repack the changed files in the ODT folder into a new odt file.
    ; Syntax.........: _ODTRepack($sTarget, $sBasefile, $sNewfile)
    ; Parameters.....: $sTarget - Folder of operation
    ; $sBasefile - original ODT file for keeping compression
    ; $sNewfile - [optional] filename of the new ODT file. Default: $sBasefile
    ;
    ; Return values..: Success - 0
    ; Failure - -1 if something went wrong
    ; Author.........: Johannes Mitlmeier
    ; Modified.......: 2011-03-25
    ; Remarks........:
    ; Related........:
    ; Link...........:
    ; Example........:
    ; ===============================================================================================================
    Func _ODTRepack($sTarget, $sBasefile, $sNewfile = "same")
    If $sNewfile = "same" Then $sNewfile = $sBasefile
    Local $command = '"' & @ScriptDir & '\7za.exe" a "' & $sNewfile & '" "' & $sTarget & '\*" -tzip -mx0 -r -y'
    $command = '"' & @ScriptDir & '\7za.exe" u "' & $sNewfile & '" "' & $sTarget & '\*" "-xr@' & $sTarget & '\mimetype" -tzip -mx0 -r -y'
    If $sNewfile <> $sBasefile Then FileCopy($sBasefile, $sNewfile)
    RunWait($command)
    If FileExists($sNewfile) Then Return 0
    Return -1
    EndFunc ;==>_ODTRepack

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    ;### internal functions

    [/autoit] [autoit][/autoit] [autoit]

    Func _ODTWriteToFile($sHandle, $sFilename, $sContent)
    Local $hFile = FileOpen($sHandle & "\" & $sFilename, 2 + 128)

    [/autoit] [autoit][/autoit] [autoit]

    ; Check if file opened for writing OK
    If $hFile = -1 Then
    SetError(@error, 0, -1)
    EndIf
    FileWrite($hFile, $sContent)
    FileClose($hFile)

    [/autoit] [autoit][/autoit] [autoit]

    Return 0
    EndFunc ;==>_ODTWriteToFile

    [/autoit]

    Beispielskript:

    Spoiler anzeigen
    [autoit]

    #cs ----------------------------------------------------------------------------

    [/autoit] [autoit][/autoit] [autoit]

    AutoIt Version: 3.3.6.1
    Author: Johannes Mitlmeier

    [/autoit] [autoit][/autoit] [autoit]

    Script Function:
    Demonstrate ODT UDF functions.

    [/autoit] [autoit][/autoit] [autoit]

    #ce ----------------------------------------------------------------------------

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    #include "odt.au3"

    [/autoit] [autoit][/autoit] [autoit]

    ; Laden
    $odt = _ODTUnpack(@ScriptDir & "\testfile.odt", @ScriptDir & "\testdir")
    $content = _ODTGetContent($odt)

    [/autoit] [autoit][/autoit] [autoit]

    ; Ändern
    $content = StringReplace($content, "mir", @UserName)
    $content = StringReplace($content, '<style:style style:name="T1" style:family="text"><style:text-properties fo:font-weight="bold" ', '<style:style style:name="T1" style:family="text"><style:text-properties fo:font-weight="normal" ')

    [/autoit] [autoit][/autoit] [autoit]

    ; Speichern
    _ODTSetContent($odt, $content)
    _ODTRepack(@ScriptDir & "\testdir", @ScriptDir & "\testfile.odt", @ScriptDir & "\testfile2.odt")

    [/autoit] [autoit][/autoit] [autoit]

    ; Starten
    ShellExecute(@ScriptDir & "\testfile2.odt")

    [/autoit]

    Alle Daten als Paket zum Download im Anhang.

    Möglicherweise ist die XML-UDF nützlich, um die ausgepackten XML-Dateien bequem zu bearbeiten:
    http://code.google.com/p/my-autoit/do…LDomWrapper.au3


    Über Testberichte und Anregungen freue ich mich :).

    Viel Spaß damit,
    Johannes

  • Hallo peethebee,

    dein Beispiel läuft auch bei mir einwandfrei. Jetzt wollte ich dass File automatisch ausdrucken:

    [autoit]

    ShellExecute(@ScriptDir & "\testfile2.odt","","print")

    [/autoit]

    hast du eine Idee, warum das nicht klappt?

    mfg autoBert