[Gambas-user] How do I manipulate binary files?

Benoit Minisini gambas at ...1...
Mon Oct 29 15:21:44 CET 2007


On lundi 29 octobre 2007, Fredrik Hansson wrote:
> Hi,
>
> 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
> Windows...
>
> 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
> example).
>
> 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
> file?
>
>   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
>     (etc)
>
> Anyone have a clue?
> /Fredrik
>
>

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 << ' >> 
for example.

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 :-)

Regards,

-- 
Benoit Minisini




More information about the User mailing list