- 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:
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
#cs ----------------------------------------------------------------------------
AutoIt Version: 3.3.6.1
Author: Johannes Mitlmeier
Script Function:
OpenDocument (LibreOffice/OpenOffice) function library.
Version 0.1
#ce ----------------------------------------------------------------------------
#include-once
#include <File.au3>
; #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
; #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
; #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
Func _ODTGetStyles($_handle)
Return FileRead($_handle & "\styles.xml")
EndFunc ;==>_ODTGetStyles
Func _ODTSetStyles($_handle, $_styles)
EndFunc ;==>_ODTSetStyles
; #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
;### internal functions
[/autoit] [autoit][/autoit] [autoit]Func _ODTWriteToFile($sHandle, $sFilename, $sContent)
Local $hFile = FileOpen($sHandle & "\" & $sFilename, 2 + 128)
; Check if file opened for writing OK
If $hFile = -1 Then
SetError(@error, 0, -1)
EndIf
FileWrite($hFile, $sContent)
FileClose($hFile)
Return 0
EndFunc ;==>_ODTWriteToFile
Beispielskript:
Spoiler anzeigen
#cs ----------------------------------------------------------------------------
[/autoit] [autoit][/autoit] [autoit]AutoIt Version: 3.3.6.1
Author: Johannes Mitlmeier
Script Function:
Demonstrate ODT UDF functions.
#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)
; Ä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" ')
; Speichern
_ODTSetContent($odt, $content)
_ODTRepack(@ScriptDir & "\testdir", @ScriptDir & "\testfile.odt", @ScriptDir & "\testfile2.odt")
; Starten
ShellExecute(@ScriptDir & "\testfile2.odt")
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