Skip to content

Instantly share code, notes, and snippets.

@albcunha
Last active February 7, 2017 20:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save albcunha/65499672df1508f329a199ef4ca64a84 to your computer and use it in GitHub Desktop.
Save albcunha/65499672df1508f329a199ef4ca64a84 to your computer and use it in GitHub Desktop.
Macro script to rename file on libre office. You should put this file on "C:\Program Files (x86)\LibreOffice 5\share\Scripts\python" and insert it as macro on toolbar.
import os
from os.path import join, realpath
import zipfile
from shutil import copyfile
import uno
from com.sun.star.beans import PropertyValue
from com.sun.star.awt.PosSize import POSSIZE
def addAwtModel(oDM,srv,sName,dProps):
oCM = oDM.createInstance("com.sun.star.awt.UnoControl"+ srv +"Model")
while dProps:
prp = dProps.popitem()
uno.invoke(oCM,"setPropertyValue",(prp[0],prp[1]))
#works with awt.UnoControlDialogElement only:
oCM.Name = sName
oDM.insertByName(sName,oCM)
def Rename( ):
'''Rename Documents'''
def UserFields(model, nome, valor):
### altera o valor do campo nome.
master = model.TextFieldMasters.getByName(FIELDMASTER_TEMPLATE % nome)
master.Content = valor
# cria uma instancia que permite a insercao no texto
objfield = model.createInstance("com.sun.star.text.textfield.User")
objfield.attachTextFieldMaster(master)
return objfield
context = uno.getComponentContext()
smgr = context.ServiceManager
desktop = smgr.createInstanceWithContext("com.sun.star.frame.Desktop",context)
document = desktop.getCurrentComponent()
url = document.URL
syspath = uno.fileUrlToSystemPath(url)
texto_arquivo = str(syspath)
arquivo = texto_arquivo[texto_arquivo.rfind('\\')+1:]
caminho = texto_arquivo[:texto_arquivo.rfind('\\')+1]
#faz dialogo
oDM = smgr.createInstance("com.sun.star.awt.UnoControlDialogModel")
oDM.Title = 'Renomear arquivo:'
addAwtModel(oDM,'FixedText','lblName',{
'Label' : 'Arquivo: ',
}
)
addAwtModel(oDM,'Edit','txtName',{
'Text':arquivo})
addAwtModel(oDM,'Button','btnOK',{
'Label' : 'OK',
'DefaultButton' : True,
'PushButtonType' : 1,
}
)
addAwtModel(oDM,'Button','btnCancel',{
'Label' : 'Cancel',
'PushButtonType' : 2,
}
)
oDialog = smgr.createInstance("com.sun.star.awt.UnoControlDialog")
oDialog.setModel(oDM)
txtN = oDialog.getControl('txtName')
h = 25
y = 20
for c in oDialog.getControls():
c.setPosSize(10, y, 400, h, POSSIZE)
y += h
oDialog.setPosSize(300,300,500,y+h,POSSIZE)
oDialog.setVisible(True)
x = oDialog.execute()
if x:
pass
else:
return False
arquivo_novo = caminho + txtN.getText()
oDM.dispose()
oDialog.dispose()
def urlify(path):
return uno.systemPathToFileUrl(os.path.realpath(path))
# NOTE THAT ARGS IS A TUPLE OF PROPERTY VALUES
document.storeToURL(
urlify(arquivo_novo), ())
document.dispose()
desktop.loadComponentFromURL(
urlify(arquivo_novo), "_blank", 0, ())
os.remove(str(syspath))
return None
g_exportedScripts = Rename,
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment