Multiple file conversion that outputs to the original file's folder

I have a folder called X which inside has many folders and sub-folders. I want to search in all of the folders inside X and find a specific document type. For example xlsx and transform each file into an xls file. In this case I need to use the libreoffice convert option like libreoffice --headless --convert-to xls but here is the catch. After conversion each converted file should be in the same folder as the original xlsx file. All sub-folders have random names and may have other sub-folders.

Example:

X/
 Folder 1/
         file.xlsx
 Folder 2/
         Folder 2.1/file.xlsx
 Folder 3/
 Folder 4/
         Folder 4.1/anotherFile.xlsx

After conversion:

X/
 Folder 1/
         file.xls
         file.xlsx
 Folder 2/
         Folder 2.1/file.xls
         Folder 2.1/file.xlsx
 Folder 3/
 Folder 4/
         Folder 4.1/anotherFile.xls
         Folder 4.1/anotherFile.xlsx

The new xls* in this case, will be converted on the same folder as the original xlsx. This will be done inside all sub-folders and to all xlsx that are found. So after the conversion finishes, all xlsx files will have their respective xls files beside them.

I should mention that this is not the same as others questions here about conversion because:

  • I don’t want to output all files to the same folder. They should each go inside the same folder as their converted file.
  • They should not be converted and output in the working folder. The working folder where I execute the command is one thing. The output of each file is in their respective converted file’s folder.
  • If this is not available with the current libreoffice command, a for, find or any other Linux command can be used. This should run on an Linux OS like Ubuntu.

Using python would be pretty straightforward, useful modules are subprocess and glob.

#!/usr/bin/python

from glob import glob
from subprocess import call
root = './X'
for depth in range(1, 10):
    for path in glob('%s%s.xlsx' % (root, '/*' * depth)):
        call(['libreoffice', '--headless', '--convert-to', 'xls', path])