我有一个程序,可以创建 CSV 文件。
现在我想使用 Python UNO 脚本,它可以完成几件事:
1.) 在电子表格中打开 csv 文件
2.) 为所有列启用自动过滤器
3.) 创建宏并将其添加到文档
4.) 将文件另存为 ODS 文件
这个问题只涉及2.)
1.) 正在工作
对于 3.)我可能会提出另一个问题1.) 正在工作(与 pyoo 和 unotools 一起)
到目前为止我的步骤:
我手动启动:
libreoffice --accept='socket,host=localhost,port=2202;urp;' --norestore --nologo --nodefault
我的Python脚本:
与pyoo
import pyoo
# Step 0) connect to UNO bridge
desktop = pyoo.Desktop('localhost', 2002)
# Step 1) open the doc and get the sheet
# This works only if the field separator is a comma.
# I don't know how for example to specify tab as separator instead
doc = desktop.open_spreadsheet('tst.csv')
# I see the spreadsheet opening
sheet = doc.sheets[0] # I get an object of type Sheet
# Step2) set autofilter for active sheet
# no idea how to do
# Step3) create a macro and add it to the document
# no idea how to do but will create another question as
# soon as step2 is solved
# Step 4) save the sheet
doc.save("tst_pyoo.ods")
或者使用 unotools
import unotools
from unotools.component.calc import Calc
from unotools.unohelper import convert_path_to_url
# Step 0) connect to UNO bridge
context = unotools.connect(unotools.Socket('localhost', 2002))
# Step 1) open document
doc = Calc(ctx, convert_path_to_url('tst.csv')
# I see the spreadsheet opening
sheet = doc.get_sheet_by_index(0)
# I get an object of type unotools.component.calc.Spreadsheet
# Step2) set autofilter for active sheet
# no idea how to do
# Step3) create a macro and add it to the document
# no idea how to do but will create another question as
# soon as step2 is solved
# Step 4)
doc.store_to_url(convert_path_to_url("tst_unotools.ods"))
预先感谢您的任何反馈
这里是使用直接 PyUNO 而不是包装器库的代码。改编自http://www.imaccanici.org/en.libreofficeforum.org/node/5413.html .
import os
import uno
class UnoObjs:
"Initializes and stores UNO objects to connect to a document."""
def __init__(self, filepath=None):
localContext = uno.getComponentContext()
resolver = localContext.ServiceManager.createInstanceWithContext(
"com.sun.star.bridge.UnoUrlResolver", localContext )
self.ctx = resolver.resolve(
"uno:socket,host=localhost,port=2002;urp;"
"StarOffice.ComponentContext")
self.smgr = self.ctx.ServiceManager
desktop = self.smgr.createInstanceWithContext(
"com.sun.star.frame.Desktop", self.ctx)
if filepath:
fileUrl = uno.systemPathToFileUrl(os.path.realpath(filepath))
self.document = desktop.loadComponentFromURL(
fileUrl, "_default", 0, ())
else:
self.document = desktop.getCurrentComponent()
def add_autofilter(unoObjs):
"""This adds an autofilter by selecting only the filled spreadsheet area.
NOTE: If any cell in the header row of the selection is empty this will
trigger a popup for interactive user action (must click Yes for the
Autofilter column header message box)
"""
dispatcher = unoObjs.smgr.createInstanceWithContext(
"com.sun.star.frame.DispatchHelper", unoObjs.ctx)
controller = unoObjs.document.getCurrentController()
sheet = unoObjs.document.getSheets().getByIndex(0)
# select a sufficiently big "guess" area, hopefully
guessRange = sheet.getCellRangeByPosition(0, 0, 150, 10000)
# look up the actual used area within the guess area
cursor = sheet.createCursorByRange(guessRange)
cursor.gotoEndOfUsedArea(False)
lastcol = cursor.RangeAddress.EndColumn
lastrow = cursor.RangeAddress.EndRow
# select the filled part of the spreadsheet
actualRange = sheet.getCellRangeByPosition(0, 0, lastcol, lastrow)
controller.select(actualRange)
# add autofilter
dispatcher.executeDispatch(
unoObjs.document.getCurrentController(), ".uno:DataFilterAutoFilter",
"", 0, ())
add_autofilter(UnoObjs("tst.csv"))
诸如.uno:DataFilterAutoFilter
之类的调度程序调用很难找出参数。在大多数情况下,最好使用 UNO API 调用,例如 XTextCursor反而。但是,有一些选项可以确定调度程序调用:
- 使用宏录制器。
- 查看类似 this one 的列表.
- 在 LibreOffice 源代码中查找调用。这是最可靠的,但有时仍然很难确定论点。
关于调度员调用,请参阅https://forum.openoffice.org/en/forum/viewtopic.php?f=20&t=61127 .
我是一名优秀的程序员,十分优秀!