如何使用Python Uno打开XLS文件并将其另存为CSV作为UTF-8
使用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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试以这种方式重写:
Try rewriting this way: