我使用pyoo作为打开的文档电子表格生成报告。pyoo可以完成我需要的所有设置列宽度的操作。有些我想设置为常数,另一些作为最佳宽度。来自pyoo网站(https://github.com/seznam/pyoo):“如果缺少一些重要的特性,那么UNO总是可用的。”
经过几个小时的谷歌搜索,我来到了com.sun.star.table.TableColumn类,此页的这个类似乎有我所需要的属性(“宽度”和"OptimalWidth"),但是-
>>> x = uno.getClass('com.sun.star.table.TableColumn')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3/dist-packages/uno.py", line 114, in getClass
return pyuno.getClass(typeName)
uno.RuntimeException: pyuno.getClass: uno exception com.sun.star.table.TableColumn is unknown我根本不知道怎么让这件事起作用。UNO的文件太多了,至少可以说.
任何线索都将不胜感激。
发布于 2018-04-28 14:30:01
示例Python-UNO代码:
def resize_spreadsheet_columns():
oSheet = XSCRIPTCONTEXT.getDocument().getSheets().getByIndex(0)
oColumns = oSheet.getColumns()
oColumn = oColumns.getByName("B")
oColumn.IsVisible = False
oColumn = oColumns.getByName("C")
oColumn.Width = 7000
oColumn = oColumns.getByName("D")
oColumn.OptimalWidth = True文档:
编辑
从评论中可以看出,您需要完成关于Python的入门教程。试试http://christopher5106.github.io/office/2015/12/06/openoffice-libreoffice-automate-your-office-tasks-with-python-macros.html。
发布于 2018-05-01 14:46:05
谢谢吉姆·K,你给我指明了正确的方向,我让它运转起来了。我的python脚本生成了一个十个工作表电子表格,手动调整列宽变得很麻烦。如果有人想要评论或者需要解决这个问题,我会在我的最后代码下面发布。在我看来,这就像是把生的UNO调用和pyoo结合在一起的猪早餐,但我认为它很管用。
#! /usr/bin/python3.6
import os, pyoo, time, uno
s = '-'
while s != 'Y':
s = input("Have you remembered to start Calc? ").upper()
os.system("soffice --accept=\"socket,host=localhost,port=2002;urp;\" --norestore --nologo --nodefault")
time.sleep(2)
desktop = pyoo.Desktop('localhost', 2002)
doc = desktop.create_spreadsheet()
class ofic:
sheet_idx = 0
row_num = 0
sheet = None
o = ofic()
uno_localContext = uno.getComponentContext()
uno_resolver = uno_localContext.ServiceManager.createInstanceWithContext("com.sun.star.bridge.UnoUrlResolver", uno_localContext )
uno_ctx = uno_resolver.resolve( "uno:socket,host=localhost,port=2002;urp;StarOffice.ComponentContext" )
uno_smgr = uno_ctx.ServiceManager
uno_desktop = uno_smgr.createInstanceWithContext( "com.sun.star.frame.Desktop", uno_ctx)
uno_model = uno_desktop.getCurrentComponent()
uno_controller = uno_model.getCurrentController()
uno_sheet_count = 0
for i in range(5):
doc.sheets.create("Page {}".format(i+1), index=o.sheet_idx)
o.sheet = doc.sheets[o.sheet_idx]
o.sheet[0, 0].value = "The quick brown fox jumps over the lazy dog"
o.sheet[1, 1].value = o.sheet_idx
uno_controller.setActiveSheet(uno_model.Sheets.getByIndex(uno_sheet_count))
uno_sheet_count += 1
uno_active_sheet = uno_model.CurrentController.ActiveSheet
uno_columns = uno_active_sheet.getColumns()
uno_column = uno_columns.getByName("A")
uno_column.OptimalWidth = True
uno_column = uno_columns.getByName("B")
uno_column.Width = 1350
o.sheet_idx += 1
doc.save("whatever.ods")
doc.close()https://stackoverflow.com/questions/50065976
复制相似问题