用于 Libreoffice-calc 和 Openoffice-calc 的 Python“Hello World”[关闭]
Posted
技术标签:
【中文标题】用于 Libreoffice-calc 和 Openoffice-calc 的 Python“Hello World”[关闭]【英文标题】:Python "Hello World" for Libreoffice-calc and Openoffice-calc [closed] 【发布时间】:2015-04-27 09:03:01 【问题描述】:Libre/Openoffice-writer 有一个 python-helloworld 示例(包含在 Libreoffice 4.1.6.2 中:
def HelloWorldWriter( ):
"""Prints the string 'Hello World(in Python)' into the current document"""
#get the doc from the scripting context which is made available to all scripts
desktop = XSCRIPTCONTEXT.getDesktop()
model = desktop.getCurrentComponent()
#check whether there's already an opened document. Otherwise, create a new one
if not hasattr(model, "Text"):
model = desktop.loadComponentFromURL(
"private:factory/swriter","_blank", 0, () )
#get the XText interface
text = model.Text
#create an XTextRange at the end of the document
tRange = text.End
#and set the string
tRange.String = "Hello World (in Python)"
return None
该脚本检查打开的编写器文档,如果不存在则创建一个新文档,并将字符串输出到该文档中。
Libreoffice/Openoffice-calc 有类似的东西吗?
理想情况下,它应该包括:
· Read a table cell
· Write a table cell
· Save as ODT/XLS/CSV
【问题讨论】:
【参考方案1】:对于开放式办公室,请检查: http://stuvel.eu/ooo-python : http://www.apidev.fr/blog/2011/07/18/utiliser-openoffice-avec-python/ 解释是法语,但请检查代码:
import os, sys
if sys.platform == 'win32':
#This is required in order to make pyuno usable with the default python interpreter under windows
#Some environment varaible must be modified
#get the install path from registry
import _winreg
value = _winreg.QueryValue(_winreg.HKEY_LOCAL_MACHINE, 'SOFTWARE\OpenOffice.org\UNO\InstallPath')
install_folder = '\\'.join(value.split('\\')[:-1])
#modify the environment variables
os.environ['URE_BOOTSTRAP'] = 'vnd.sun.star.pathname:0\\program\\fundamental.ini'.format(install_folder)
os.environ['UNO_PATH'] = install_folder+'\\program\\'
sys.path.append(install_folder+'\\Basis\\program')
paths = ''
for path in ("\\URE\\bin;", "\\Basis\\program;"):
paths += install_folder + path
os.environ['PATH'] = paths+ os.environ['PATH']
import uno
使用计算:
class UnoClient:
def __init__(self):
localContext = uno.getComponentContext()
resolver = localContext.ServiceManager.createInstanceWithContext(
"com.sun.star.bridge.UnoUrlResolver", localContext)
self.smgr = resolver.resolve("uno:socket,host=localhost,port=2002;urp;StarOffice.ServiceManager")
def create_document(self, app):
remoteContext = self.smgr.getPropertyValue("DefaultContext")
desktop = self.smgr.createInstanceWithContext( "com.sun.star.frame.Desktop",remoteContext)
url = "private:factory/0".format(app)
return desktop.loadComponentFromURL(url,"_blank", 0, () )
你应该这样使用它:
calc = UnoClient().create_document('scalc') #cree un nouveau classeur
sheet = calc.getSheets().getByIndex(0) #1ere feuille du classeur
sheet.getCellByPosition(0, 0).setString("Salut") #Un texte
sheet.getCellByPosition(0, 1).setValue(3.14) #Un nombre
sheet.getCellByPosition(0, 2).setFormula("=SUM(2+2)") #Une formule
sheet.getCellByPosition(0, 2).CellBackColor = int("ff7f00", 16) #Couleur RGB de fond
sheet.getCellByPosition(0, 2).CharUnderline = 1 # Souligne
sheet.getCellByPosition(0, 2).CharHeight = 16 #Taille de la police
sheet.getCellByPosition(0, 2).CharWeight = 150 #Gras
sheet.getCellByPosition(0, 2).CharPosture = 2 #Italique
检查一下! http://oosheet.hacklab.com.br/
【讨论】:
以上是关于用于 Libreoffice-calc 和 Openoffice-calc 的 Python“Hello World”[关闭]的主要内容,如果未能解决你的问题,请参考以下文章
用于 CSV 导入的 Libreoffice Calc 模板
Sql Server JDBC 不适用于 Wildfly AS