When I edit a spreadsheet with pyexcel it loses its appearance

2

Hello, when I try to add new values to a ods worksheet, it loses all its "style" and is left with values only. Follow the code:

import pyexcel

sheet = pyexcel.get_sheet(file_name="dados.ods")
sheet.row += [0, 0, 0]
sheet.save_as("example_series_filter.ods")

It works perfectly, but as I said I miss the appearance of the worksheet PS: use python3 and have the libraries properly installed

    
asked by anonymous 16.06.2016 / 14:30

1 answer

1

You are in the PyExcel package description -

  

Known constraints:

     

Fonts, colors and charts are not supported.

That is, this package, although convenient, currently simply discards the appearance information of the worksheet.

You might find another package that preserves the information - but unfortunately, although there are other packages, the focus on manipulating spreadsheets is always getting in the numbers - and anything goes for the looks.

One thing that would probably work there is you manipualr manually some aspects of the ODS file - the ODS, as well as the XLSX are zip files with XML files and images aggregated inside. These pythons uses Python's ziplib and xml tools to manipulate those worksheets. The complicated logic is how to maintain the structure of these XML where the data is.

Now, if you lsiatr the contents of an ods file with a tool like "unzip", you will see several .xml files in there. Part of the formatting information the .ods file is in the other .xml files - while the numeric content itself is always in the content.xml file. The pyexcel package probably just re-creates the entire ODS file from scratch, and thus creates the other xml ( styles.xml , 'meta.xml), etc ...) blank or the least possible content to be valid.

You can do it this way - I will not put the complete Python code because I would need to do some tests and it would be a lot of work (remember that the answers here are always voluntary) - but you can do it there - doing the tests in the same interactive mode

  • copy the .ods file to another, creating a backup (use the Python shutil)
  • manipulate the original file as pyexcel exactly as you are doing now
  • Use Python's ziplib to get the contents.xml file inside the file written by pyexcel, and put it inside the file copied in the first step.

Ready - at least some style information - those that are not directly inline in the contents.xml will be preserved. (The problem is that in my experience much of the formatting information is within the same contents.xml - but if you do so far, you soon begin to have enough confidence to mnipualr the xml inside the content.xml directly).

Another alternative is to use a Python script that uses LibreOffice itself - in this case, instead of manipulating the file directly, you use the Libreoffice API calls (called "PyUno") to directly control the program, and insert the data into an existing worksheet. Libreoffice, claor, has full support for all spreadsheet functionalities - not just formatting, graphics, etc ... and would preserve anything you do not touch.

    
16.06.2016 / 20:30