0

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?

Miranda
  • 148
  • 13
  • Have you tried `document = desktop.loadComponentFromURL("file:///home/miranda/Desktop/Crime_CSV/myfile.xls", "_blank", 0, inProps )`? – MattDMo Apr 15 '22 at 23:10
  • @MattDMo OMG, yes! Thank you! Now I have to find out how to save it as csv. – Miranda Apr 15 '22 at 23:18
  • 1
    No prob. I got some hints googling `libreoffice uno python save document as csv`, so hopefully you can patch enough of it together to get some working code. I don't have LibreOffice, so unfortunately I can't help there. Good luck! – MattDMo Apr 15 '22 at 23:41
  • 1
    `Dim` is Basic language. In Python it is like this: `from com.sun.star.beans import PropertyValue`, then `ruleProps = (PropertyValue("Adjust", 0, 2, 0),)` -- be sure to use tuples and not arrays. Also is there a reason that you want to do this in Python rather than a `--convert-to` command line argument? – Jim K Apr 16 '22 at 12:42

1 Answers1

0

Try rewriting this way:

filter0 = uno.createUnoStruct('com.sun.star.beans.PropertyValue')
filter0.Name = 'FilterName'
filter0.Value = 'Text - txt - csv (StarCalc)'
filter1 = uno.createUnoStruct('com.sun.star.beans.PropertyValue')
filter1.Name = "FilterOptions"
filter1.Value = "44,34,76,1,,1031,true,true"

filters = (filter0,filter1,)
mapto
  • 605
  • 9
  • 23