Using python uno, I am trying to open xls files in LibreOffice, and then save them as csv. I must use LibreOffice to open the files, because they are corrupted. I cannot simply open them using pandas and then save as csv =(
This is what I tried:
In the command prompt (Linux mint-Una):
/usr/lib/libreoffice/program/soffice.bin --headless --invisible --nocrashreport --nodefault --nofirststartwizard --nologo --norestore --accept='socket,host=localhost,port=2002,tcpNoDelay=1;urp;StarOffice.ComponentContext'
Following this post (Using Python to access LibreOffice Calc using Uno), I wrote the following python script:
import uno
local_ctx = uno.getComponentContext()
smgr_local = local_ctx.ServiceManager
resolver = smgr_local.createInstanceWithContext("com.sun.star.bridge.UnoUrlResolver", local_ctx)
url = "uno:socket,host=localhost,port=2002,tcpNoDelay=1;urp;StarOffice.ComponentContext"
uno_ctx = resolver.resolve(url)
uno_smgr = uno_ctx.ServiceManager
desktop = uno_smgr.createInstanceWithContext("com.sun.star.frame.Desktop", uno_ctx )
PropertyValue = uno.getClass('com.sun.star.beans.PropertyValue')
inProps = PropertyValue( "Hidden" , 0 , True, 0 ), # this is a tuple
document = desktop.loadComponentFromURL("file:///home/miranda/Desktop/Crime_CSV/myfile.xls", "_blank", 0, inProps )
import os
path = os.path.abspath('./newfile.csv')
uno_url = uno.systemPathToFileUrl(path)
So far, so good, the problem is when I try to save it as a UTF-8 csv file. This is what I tried, based on this forum (https://forum.openoffice.org/en/forum/viewtopic.php?f=20&t=79147):
Dim filter(2) as new com.sun.star.beans.PropertyValue
filter(0).Name = 'FilterName'
filter(0).Value = 'Text - txt - csv (StarCalc)'
filter(1).Name = "FilterOptions"
filter(1).Value = "44,34,76,1,,1031,true,true"
filters = (filter,)
I get the following error:
Dim filter(2) as new com.sun.star.beans.Property
^
SyntaxError: invalid syntax
How do I save as both csv and UTF-8?