On lundi 29 octobre 2007, Fredrik Hansson wrote:
> I would like to write chars (bytes) directly into an OpenOffice file.
> The file is a document, a template, and at specific places I want to put
> in the right letters. In that way my application can fill in a form made
> in OpenOffice automatically. An old trick that used to work in
> But here's the problem: I read byte by byte from the document file.
> I look for specific words there (by combining the last 4 bytes for
> I have put field markers in the document, like H#01, but I can't find
> them when I'm reading the bytes. Shouldn't 'H' have the code 72 and '#'
> the code 35 for example? Or is that old ASCII not used in
> Linux/OpenOffice? Or am I using the wrong method when I try to read the
> DIM b AS Byte
> DIM fil AS file
> fil = OPEN "document.odt" FOR READ
> WHILE NOT Eof(fil)
> READ #fil, b
> IF b = 72 THEN 'look for the letter H
> Anyone have a clue?
OpenOffice files are actually zip archives of UTF-8 XML text files (and binary
files for images or other binary formats).
So to do what you want, you should better unzip the *.odt file, and just
replace your markups by the real values in the contents.xml file.
Be careful with OpenOffice XML: strings are encoded in UTF-8, and some
characters uses XML entities, like in HTML: << "e; >> instead of << ' >>
Moreover, newlines must be inserted with a special OpenOffice markup. I let
you discover that by yourself :-)
Once done, you just have to zip back all unzipped files, and you get a
perfectly valid OpenOffice document.
I use that for my job in a software written in Gambas that, among many other
things, can generate OpenOffice documents and spreadsheets, that can be
translated to PDF, Word and Excel too.
Logically, after having done that, you will ask why people still use Word and
Excel in a professional environment :-)