Remove file password from command line

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
siemaeniu
Posts: 4
Joined: Mon Dec 23, 2013 4:03 pm

Remove file password from command line

Post 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 ;)
LibreOffice 3.5.7.2 Ubuntu 12.04 LTS
FJCC
Moderator
Posts: 9248
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: Remove file password from command line

Post 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?
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
siemaeniu
Posts: 4
Joined: Mon Dec 23, 2013 4:03 pm

Re: Remove file password from command line

Post 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

        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

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.
LibreOffice 3.5.7.2 Ubuntu 12.04 LTS
User avatar
RoryOF
Moderator
Posts: 34586
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: Remove file password from command line

Post 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?
Apache OpenOffice 4.1.15 on Xubuntu 22.04.4 LTS
siemaeniu
Posts: 4
Joined: Mon Dec 23, 2013 4:03 pm

Re: Remove file password from command line

Post 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.
LibreOffice 3.5.7.2 Ubuntu 12.04 LTS
FJCC
Moderator
Posts: 9248
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: Remove file password from command line

Post 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

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)
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
siemaeniu
Posts: 4
Joined: Mon Dec 23, 2013 4:03 pm

Re: Remove file password from command line

Post 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"?
LibreOffice 3.5.7.2 Ubuntu 12.04 LTS
User avatar
RoryOF
Moderator
Posts: 34586
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: Remove file password from command line

Post by RoryOF »

Try it and see! If it doesn't work, then install OpenOffice.
Apache OpenOffice 4.1.15 on Xubuntu 22.04.4 LTS
FJCC
Moderator
Posts: 9248
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: Remove file password from command line

Post 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

prop = uno.createUnoStruct('com.sun.star.beans.PropertyValue') 
as shown by Villeroy in this thread
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
rudolfo
Volunteer
Posts: 1488
Joined: Wed Mar 19, 2008 11:34 am
Location: Germany

Re: Remove file password from command line

Post by rudolfo »

I gave it a go with the macro recorder and figured out that for DocBook you need something like:

Code: Select all

  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.
OpenOffice 3.1.1 (2.4.3 until October 2009) and LibreOffice 3.3.2 on Windows 2000, AOO 3.4.1 on Windows 7
There are several macro languages in OOo, but none of them is called Visual Basic or VB(A)! Please call it OOo Basic, Star Basic or simply Basic.
bluebricktech
Posts: 1
Joined: Mon Oct 06, 2014 3:53 am

Re: Remove file password from command line

Post by bluebricktech »

Hey FJCC,
Could you please help me get your script working for removing the passwords on Documents
OpenOffice 4.1.0 Windows 7
Post Reply