Edit: I found the error in my Office class and fixed the code block. |
The following script works when I run it as a macro
but fails when I run it as a stand-alone script ("memory access error" in loadComponentFromURL) and as stand-alone script as well.
There are 4 global variables:
1. g_Connection which is not used when the code is run in macro context. See comment text in the code.
2. g_Pattern = '/tmp/test/*.od?' specifies the path and name pattern
3. g_OldPwd is the password to open the files
4. g_NewPwd is the password to store the files
Store it under ~/.config/libreoffice/4/user/Scripts/python/reencrypt.py (where ~/.config/libreoffice/4/user/ is the path of your user profile)
Start the office from a terminal and call Tools>Macros>Organize>Python... browse "MyMacros">reencrypt > reEncrypt_Macro and push the [Run] button
On the terminal you get an output for each file, either Success: filename or ###FAIL: filename
Code: Select all
import glob
import uno
from com.sun.star.awt.MessageBoxType import MESSAGEBOX, INFOBOX, WARNINGBOX, ERRORBOX, QUERYBOX
from com.sun.star.awt.MessageBoxButtons import BUTTONS_OK, BUTTONS_OK_CANCEL, BUTTONS_ABORT_IGNORE_RETRY, BUTTONS_YES_NO_CANCEL, BUTTONS_YES_NO, BUTTONS_RETRY_CANCEL, DEFAULT_BUTTON_OK, DEFAULT_BUTTON_CANCEL, DEFAULT_BUTTON_RETRY, DEFAULT_BUTTON_YES, DEFAULT_BUTTON_NO, DEFAULT_BUTTON_IGNORE
from com.sun.star.awt.MessageBoxResults import CANCEL, OK, YES, NO, RETRY, IGNORE
#################################
'''
if you call the office suite like this:
soffice --accept="socket,host=localhost,port=2002;urp;StarOffice.ServiceManager"
then
g_Connect = "uno:socket,host=localhost,port=2002;urp;StarOffice.ComponentContext"
'''
g_Connect = "uno:socket,host=localhost,port=2002;urp;StarOffice.ComponentContext"
g_Pattern = '/tmp/test/*.od?'
g_OldPwd = 'bar'
g_NewPwd = 'foo'
#################################
class Office:
'''Frequently used office objects and useful methods'''
def __init__(self, connect_string = '', local_context = uno.getComponentContext()):
if connect_string:
# soffice.bin --accept="socket,host=localhost,port=2002;urp;StarOffice.ServiceManager"
resolver = local_context.ServiceManager.createInstanceWithContext("com.sun.star.bridge.UnoUrlResolver", local_context )
self.ctx = resolver.resolve(connect_string)
else:
self.ctx = local_context
self.smgr = self.ctx.ServiceManager
self.tk = self.smgr.createInstance('com.sun.star.awt.Toolkit')
self.StarDesktop = self.smgr.createInstance('com.sun.star.frame.Desktop')
self.win = None
def getDocument(self):
return self.StarDesktop.CurrentFrame.Controller.getModel()
def createUnoService(self, name,):
return self.smgr.createInstance(name)
def getURLStruct(self, sURL):
url = uno.createUnoStruct('com.sun.star.util.URL')
srv = self.createUnoService('com.sun.star.util.URLTransformer')
url.Complete = sURL
x = uno.invoke(srv,"parseStrict",(uno.Any('com.sun.star.util.URL',url),))
return x[0]==True and x[1] or Null
def getPropertyValue(self, n, v):
p = uno.createUnoStruct('com.sun.star.beans.PropertyValue')
p.Name = n
p.Value = v
return p
def setMsgboxWindow(self, win):
self.win = win
def msgbox(self,title='<Dummy>',msg='<Hello World>',etyp=MESSAGEBOX,ebtn=BUTTONS_OK):
mbox = self.tk.createMessageBox(self.win,etyp,ebtn,title,msg)
return mbox.execute()
def mri(self, obj):
mri = self.createUnoService("mytools.Mri")
mri.inspect(obj)
def reEncrypt_Macro(**args):
reEncrypt('')
def reEncrypt(sCon):
ofc = Office(sCon)
pv1 = ofc.getPropertyValue("Password", g_OldPwd)
pv2 = ofc.getPropertyValue("Password", g_NewPwd)
dtp = ofc.StarDesktop
g = glob.iglob(g_Pattern)
for f in g:
url = uno.systemPathToFileUrl(f)
try:
doc = dtp.loadComponentFromURL(url, '_blank', 0, (pv1,))
doc.storeAsURL(url, (pv2,))
except:
print('### FAIL: ' + f)
else:
doc.close(False)
print('SUCCESS: ' + f)
if __name__ == "__main__":
reEncrypt(g_Connect)
g_exportedScripts = reEncrypt_Macro,
I ran this several times changing the passwords between "foo" and "bar" and vice versa. When the files are unencrypted, the password is ignored.
In CLI mode it
was intended to be started works like this:
Code: Select all
cd ~/.config/libreoffice/4/user/Scripts/python/
libreoffice --accept="socket,host=localhost,port=2002;urp;StarOffice.ServiceManager" &
python3 reencrypt.py
and the output is
Code: Select all
func=xmlSecCheckVersionExt:file=xmlsec.c:line=188:obj=unknown:subj=unknown:error=19:invalid version:mode=abi compatible;expected minor version=2;real minor version=2;expected subminor version=25;real subminor version=26
SUCCESS: /tmp/test/Untitled 1.ods
func=xmlSecCheckVersionExt:file=xmlsec.c:line=188:obj=unknown:subj=unknown:error=19:invalid version:mode=abi compatible;expected minor version=2;real minor version=2;expected subminor version=25;real subminor version=26
SUCCESS: /tmp/test/USA_Budget_-1789_2017.ods
func=xmlSecCheckVersionExt:file=xmlsec.c:line=188:obj=unknown:subj=unknown:error=19:invalid version:mode=abi compatible;expected minor version=2;real minor version=2;expected subminor version=25;real subminor version=26
SUCCESS: /tmp/test/musterbrief_auskunftsersuchen_und_widerruf.odt
func=xmlSecCheckVersionExt:file=xmlsec.c:line=188:obj=unknown:subj=unknown:error=19:invalid version:mode=abi compatible;expected minor version=2;real minor version=2;expected subminor version=25;real subminor version=26
SUCCESS: /tmp/test/untitled_1.ods
func=xmlSecCheckVersionExt:file=xmlsec.c:line=188:obj=unknown:subj=unknown:error=19:invalid version:mode=abi compatible;expected minor version=2;real minor version=2;expected subminor version=25;real subminor version=26
SUCCESS: /tmp/test/Logfile.odt
func=xmlSecCheckVersionExt:file=xmlsec.c:line=188:obj=unknown:subj=unknown:error=19:invalid version:mode=abi compatible;expected minor version=2;real minor version=2;expected subminor version=25;real subminor version=26
SUCCESS: /tmp/test/untitled_0.odt
func=xmlSecCheckVersionExt:file=xmlsec.c:line=188:obj=unknown:subj=unknown:error=19:invalid version:mode=abi compatible;expected minor version=2;real minor version=2;expected subminor version=25;real subminor version=26
SUCCESS: /tmp/test/Untitled 1.odt
func=xmlSecCheckVersionExt:file=xmlsec.c:line=188:obj=unknown:subj=unknown:error=19:invalid version:mode=abi compatible;expected minor version=2;real minor version=2;expected subminor version=25;real subminor version=26
SUCCESS: /tmp/test/O2_Kündigung.odt