如何使用Python Uno打开XLS文件并将其另存为CSV作为UTF-8

发布于 2025-01-21 13:50:32 字数 2134 浏览 3 评论 0原文

使用Python Uno,我正在尝试在libreoffice中打开XLS文件,然后将其保存为CSV。我必须使用libreoffice打开文件,因为它们已损坏。我不能简单地使用熊猫打开它们,然后将其保存为csv =(

这是我尝试的:

在命令提示符(linux mint -una)中:

/usr/lib/libreoffice/program/program/soffice.bin--headless--headless--headless- -Incisible -nocrashReport -nodeFault -nodeFault -NofirstStartWizard -nologo -noreStore -norestore -norestore -accept ='socket,host = localhost,port = 2002,tcpnodelay = 1; urp; urp; staroffice.componentscomponentscontext'

' 使用python来使用python访问使用uno uno )以下python脚本:

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)

到目前为止,问题是我尝试将其保存为UTF-8 CSV文件时,这是我尝试的。 openoffice.org/en/forum/viewtopic.php?f=20&t=79147“ rel =“ nofollow noreferrer”> https://forum.openoffice.org/forum/forum/viewtopic.phpic.php?f=20&queptic.p.20& ampqu.amp..t.fiewtopic.phpic.pphpic.p. = 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,)

我收到以下错误:

Dim filter(2) as new com.sun.star.beans.Property
        ^
SyntaxError: invalid syntax

如何保存为CSV和UTF-8?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

东北女汉子 2025-01-28 13:50:32

尝试以这种方式重写:

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

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,)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文