Page 1 of 1

Remove file password from command line

PostPosted: Mon Dec 23, 2013 4:10 pm
by siemaeniu
Hi all ;)

I think it is an issue related to macros, not the LO itself.

I have a file with password, say, "password".

I can open the file, enter my password and save the file with conversion to DocBook without password.

I need to accomplish the same thing in Python from command line. I already have the code to convert the document to DocBook but I have to save the file without password first.

Would anybody be so kind and write/help me in writing the macro to do that?

Merry christmas ;)

Re: Remove file password from command line

PostPosted: Tue Dec 24, 2013 5:53 am
by FJCC
If by "DocBook" you mean saving in the MS Office format, the password is not saved by default. At least that is what happened in my test. Can you post your code so we can see exactly what you are doing?

Re: Remove file password from command line

PostPosted: Tue Dec 24, 2013 12:32 pm
by siemaeniu
It is simple. I will describe the graphical way.

My code in Python download all *.DOC files from one of the websites. Then I need them to be converted to something called DocBook (*.xml). It is a Word document converted to XML so it contains e.g. a table in the form of <row><entry><para>content</para></entry></row>

To perform a conversion, I use the following Python code:

Code: Select all   Expand viewCollapse view
        command = 'libreoffice --headless --convert-to xml:"DocBook File" "%s/%s"' % (os.getcwd(),filename)
        os.system(command)


But I can't convert DOC files that contain passwords for opening. The command for the command line to give the password as a parameter is an element of the wish list. So I found out that there is a possibility - by using LibreOffice API - to open a file with a password given as a parameter.

Here is the code from the Polish version of this forum:

Code: Select all   Expand viewCollapse view
sub test()
   dim properties(0) as new com.sun.star.beans.PropertyValue
   url = convertToURL("<file_path>")
   properties(0).Name = "Password"
   properties(0).Value = "<file_password>"
   doc = StarDesktop.loadComponentFromUrl(url, "_blank", 0, properties())
end sub


In addition to this code, I was told to use something like "doc.storeAsURL" and an appropriate filter to save this as DocBook

But I need to perform this task in Python so I will need to use py-UNO.

Re: Remove file password from command line

PostPosted: Tue Dec 24, 2013 12:40 pm
by RoryOF
Why not process all the passworded files to remove the password, saving them in a new directory, then apply your conversion routine to all non passworded files?

Re: Remove file password from command line

PostPosted: Tue Dec 24, 2013 1:10 pm
by siemaeniu
Absolutely no difference - if this is available, it would be even better for me. However, as I know from the Polish version of this forum, DocBook (*.xml) is saved without password by default. I will need to check this.

If it is true, all I need is to open the protected DOC file by matching the password with the file name (different DOCs = different passwords) and save it (without password) as DocBook.

All in all, DocBook is a plain XML, so I suppose you can't lock the XML file with password.

Re: Remove file password from command line

PostPosted: Tue Dec 24, 2013 6:08 pm
by FJCC
Here is Python code for opening a password protected .doc file and resaving it without a password. I could not find a filter to save a DocBook, perhaps that is only a LibreOffice feature.
Code: Select all   Expand viewCollapse view
import uno
from com.sun.star.beans import PropertyValue


def export():
  ctx = uno.getComponentContext()

  smgr = ctx.ServiceManager

  desktop = smgr.createInstanceWithContext( "com.sun.star.frame.Desktop",ctx)
# mri(desktop)
  url = "file:///C:/Users/fxcampos/Desktop/password.doc"
  properties1 = PropertyValue()
  properties1.Name = "Password"
  properties1.Value = "password"
  properties2 = PropertyValue()
  properties2.Name = "FilterName"
  properties2.Value = "MS Word 97"
  oDoc = desktop.loadComponentFromURL(url, "_blank", 0, (properties1, properties2))
  props = PropertyValue()
  props.Name = "FilterName"
  props.Value = "MS Word 97"   
  oDoc.storeAsURL("file:///C:/Users/fxcampos/Desktop/password2.doc", (props,))

def mri( target):
  ctx = uno.getComponentContext()
  mri = ctx.ServiceManager.createInstanceWithContext("mytools.Mri",ctx)
  mri.inspect(target)

Re: Remove file password from command line

PostPosted: Wed Dec 25, 2013 12:53 pm
by siemaeniu
Thank you so much and merry christmas ;)

Tell me one more thing. Is this supposed to work in LibreOffice? Or do I need to install OOo instead of LibreOffice?

I desperately need to use this conversion to DocBook. I found something interesting:

http://www.openoffice.org/xml/xmerge/docbook/

However, I will need to read it hard to understand...

As I mention it in my footer, my LibreOffice is 3.5.*.*. I read that there are differences between 3.2, 3.4 and 3.5, not to mention 4.0.

http://stackoverflow.com/questions/8772 ... office-api

EDIT: Are there special requirements to make an import "from com.sun.star.beans import PropertyValue"?

Re: Remove file password from command line

PostPosted: Wed Dec 25, 2013 1:02 pm
by RoryOF
Try it and see! If it doesn't work, then install OpenOffice.

Re: Remove file password from command line

PostPosted: Wed Dec 25, 2013 4:04 pm
by FJCC
As Rory says, you will just have to try it. You will have to find what FilterName to pass to the storeAsURL method.
There is nothing special about importing the PropertyValue structure if you are using the Python-UNO arrangement bundled with OpenOffice. You can also create one with
Code: Select all   Expand viewCollapse view
prop = uno.createUnoStruct('com.sun.star.beans.PropertyValue')

as shown by Villeroy in this thread

Re: Remove file password from command line

PostPosted: Thu Dec 26, 2013 10:20 pm
by rudolfo
I gave it a go with the macro recorder and figured out that for DocBook you need something like:

Code: Select all   Expand viewCollapse view
  props = PropertyValue()
  props.Name = "FilterName"
  props.Value = "DocBook File"   
  oDoc.storeAsURL("file:///C:/Users/fxcampos/Desktop/password2.xml", (props,))


This was recorded with Apache OpenOffice 3.4.1, but I am pretty sure the filter name hasn't changed.

Re: Remove file password from command line

PostPosted: Tue Oct 07, 2014 12:13 am
by bluebricktech
Hey FJCC,
Could you please help me get your script working for removing the passwords on Documents