Quantcast
Channel: Apache OpenOffice Community Forum
Viewing all articles
Browse latest Browse all 1281

Macros and UNO API • [Python][PGSQL] passing dates through setDataArray

$
0
0
Hello,

I'm trying to use libreoffice as a wrapper to transfer postgreSQL data onto a templated xlsx file (work constriants) and so far i am very pleased with the many features it gives. I'm facing a few difficulties as i am relatively new to this and one blocking difficulty is pasting data.

So far i've been working with a CSV as a data source to ensure that i have the basics of I/O down and now that i've moved to importing data directly using psycopg2 i've been getting errors because of the datetime format.

a simplified version of what i've been trying (same calls, just without parametered wrappers)

Code:

data = cursor.fetchall() #returns list of tuplesnb_row = len(data)        nb_col = len(data[0]) if nb_row > 0 else 0        cell_range = sheet.getCellRangeByPosition(start_column, start_row, start_column + nb_col -1, start_row + nb_row -1)        cell_range.setDataArray(tuple(data))
gives this error

Code:

uno.com.sun.star.uno.RuntimeException: Couldn't convert 2020-07-01 to a UNO type; caught exception: <class 'AttributeError'>: 'datetime.datetime' object has no attribute 'getTypes', traceback follows 
solutions I've tried:

converting any instance of datetime in data to uno.createUnoStruct("com.sun.star.util.Date") by referencing the member data documentation in the libreoffice doc (could've been a misuse)

Code:

            if isinstance(value, datetime):                uno_date = uno.createUnoStruct("com.sun.star.util.Date")                uno_date.Year = value.year                uno_date.Month = value.month                uno_date.Day = value.day                processed_row.append(uno_date)
Threw a Runtime Exception

Another idea was just to do processed_row.append(str(value)) but that made me lose the formatting (without a manual intervention on my part after the xlsx was generated)

A Final idea was to do processed_row.append(value.date()) in hopes that python would reformat it in a way that OO likes but it threw the same runtime exception as the original issue

beyond these I've looked for similar questions on the forum and haven't found exactly what I'm looking for and so I solicite your help and remarks.

Bonus question (I will mark this thread as solved even without this as it's not a big issue but I didn't feel appropriate to open two threads in a row) :

suppose I have a formula in A2 that references say, the cell B2
and i want to copy this formula over a range A3:A15000
the way i've been doing it is by using re.sub to replace the 2 with the row index, getting all that in a tuple then using setFormulaArray over that tuple but sometimes the setFormulaArray call takes about a minute, is this normal or am I doing the wrong approach?

Statistics: Posted by TTP — Mon Dec 23, 2024 12:57 pm



Viewing all articles
Browse latest Browse all 1281

Trending Articles