Hi,
if I understand your situation correctly, it is a one time task to streamline your 300 page odts. In this case I would recommend to go through the normal api functions. The below code just shows a very simple sample for paragraph style modification.
The code also demoes, how the internal xml representation of an odt can be accessed through the AO API. In the example, it shows the sizes of content.xml and styles.xml before and after the modification through the AO API. Again, I recommend to use the API and not direct XML modification. The latter has the potential to be faster, but for a one time effort I would not take the complexity overhead
Good luck,
ms777
Sub Main
oDoc = ThisComponent
msgbox "before"
ShowContentAndStyles(oDoc)
oParaEnum = oDoc.Text.createEnumeration()
while oParaEnum.hasMoreElements
oPara = oParaEnum.NextElement
msgbox oPara.ParaStyleName
oPara.ParaStyleName = "StyleCourierBlue"
wend
msgbox "after"
ShowContentAndStyles(oDoc)
End Sub
sub ShowContentAndStyles(oDoc)
abContentXml = getZipContentAsByteArray(oDoc, "content.xml")
abStylesXml = getZipContentAsByteArray(oDoc, "styles.xml")
msgbox "content.xml len: " + UBound(abContentXml) + ", styles.xml len: " + UBound(abStylesXml)
' sContentXml = getTextInputStreamFromByteArray(abContentXml).readString(array(), true)
' msgbox sContentXml
ox = createUnoService("com.sun.star.xml.dom.DocumentBuilder")
domContent = ox.parse(getTextInputStreamFromByteArray(abContentXml))
domStyles = ox.parse(getTextInputStreamFromByteArray(abStylesXml))
oAutomaticStyles = domContent.getElementsByTagNameNS("urn:oasis:names:tc:opendocument:xmlns:office:1.0", "automatic-styles").item(0)
msgbox "Automated Styles in Content.xml:"+Chr(13)+Chr(10) + getXMLString(oAutomaticStyles)
oCustomStyles = domStyles.getElementsByTagNameNS("urn:oasis:names:tc:opendocument:xmlns:office:1.0", "styles").item(0)
msgbox "Styles in Styles.xml:"+Chr(13)+Chr(10) + getXMLString(oCustomStyles)
end sub
function getZipContentAsByteArray(oDoc as Object, sFile as String) as Object
ox = createUnoService("com.sun.star.packages.zip.ZipFileAccess")
oStorageFac = createUnoService("com.sun.star.embed.StorageFactory")
oStorage = oStorageFac.createInstance
oStream = oStorage.openStreamElement("ms777", com.sun.star.embed.ElementModes.READWRITE)
Dim storeProps(0) as new com.sun.star.beans.PropertyValue
storeProps(0).Name = "OutputStream"
storeProps(0).Value = oStream
oDoc.storeToUrl("private:stream", storeProps())
Dim args(1) as Object
args(0) = oStream
ox.initialize(args)
oContent = ox.getByname(sFile)
lLength = oContent.available()
Dim abyte(lLength) as byte
lSuccess = oContent.readBytes(abyte, lLength)
if lSuccess <> lLength then
msgbox "Houston ..."
exit function
endif
getZipContentAsByteArray = abyte
End function
function getTextInputStreamFromByteArray(aByte) as Object
oPipe = createUnoService("com.sun.star.io.Pipe")
oTextInputStream = createUnoService("com.sun.star.io.TextInputStream")
oTextInputStream.setInputStream(oPipe)
oPipe.writeBytes(aByte)
oPipe.closeOutput()
getTextInputStreamFromByteArray = oTextInputStream
end function
rem thanks to Axel Richter http://de.openoffice.info/viewtopic.php?t=66828
function getXMLString(oXMLElement as object) as string
oDocumentBuilder = createUnoService("com.sun.star.xml.dom.DocumentBuilder")
oDOMDocumentNew = oDocumentBuilder.newDocument()
oXMLElementNew = oDOMDocumentNew.importNode(oXMLElement, true)
oDOMDocumentNew.appendChild(oXMLElementNew)
oPipe = createUnoService("com.sun.star.io.Pipe")
oTextInputStream = createUnoService("com.sun.star.io.TextInputStream")
oTextInputStream.setInputStream(oPipe)
oDOMDocumentNew.setOutputStream(oPipe)
oDOMDocumentNew.start()
oPipe.closeOutput()
sXML = oTextInputStream.readString(array(), true)
getXMLString = sXML
end function
Styles.odt (15.3 KB)