我有一個程序,它創建一個CSV文件。Python UNO(libreoffice):如何爲表單啓用自動過濾器
現在,我想用一個Python腳本UNO,這將做幾件事情:
1)在電子表格
2)打開CSV文件,使所有列的自動過濾
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"))
預先感謝任何反饋
想到你的下一個問題,步驟3中的宏是否會用Basic或Python編寫? –
也可以。偏愛Python,但基本沒問題。這個宏將是一些相當基本的東西。 – gelonida
Obe更多評論。 如果我可以執行第3步然後執行宏,那麼宏可以執行設置自動過濾器。 不是很優雅,但也是一個潛在的解決方案。 – gelonida