4

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.

Example:

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

After conversion:

X/
 Folder 1/
 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.

3

Here is a python script that does as you described.

For convenience reasons, to be used with other conversion commands, I defined the filepath+name and the destination before entering it in the convert command. That also makes it easy to change the destination to something else than the file's current directory (if for some reason you might need that).

note: In some cases (like mine), the command

libreoffice --headless --convert-to xls

only works when using sudo. If that is the case, you need to change ownership of ~/.config with the command:

sudo chown -R --reference="$HOME" ~/.config

as described here.

The script:

#!/usr/bin/python3

convert_dir = "/path/to/folder/tobeconverted"
import os
import subprocess

for root, dirs, files in os.walk(convert_dir):
    for name in files:
        if name.endswith(".xlsx"):
            # filepath+name
            file = root+"/"+name
            destination = root
            subprocess.Popen(["libreoffice", "--headless", "--convert-to", "xls", file, "--outdir", destination])
        else:
            pass

Copy it into an empty textfile, replace the directory in the top of the file, save it with the .py extension and run it with the command:

python3 /path/to/scrip/script.py

But I am pretty sure you know that. :)

| improve this answer | |
  • Both answers are excellent, my only doubt is why this script is about 5x slower than the bash script below. I was under the impression that python would be faster. Do you have any idea? Could it be the 2 for and an additional if? – Luis Alvarado May 15 '14 at 14:37
  • 1
    @LuisAlvarado The time the script itself takes is practically none, compared to the conversion time libreoffice takes. However, because I used subprocess.call in the command loop, the script waited for each conversion to finish before proceeding. When subprocess.Popen is used, the script continues after sending the command. In practice this will not be a problem, but I can imagine in case of a directory with thousands of files to convert on an older system, it could go wrong if libreoffice is battered with too many requests in a short time. I changed it, this one is drastically faster. – Jacob Vlijm May 15 '14 at 16:16
  • Well that was dramatically faster! – Luis Alvarado May 15 '14 at 16:32
3

This works for me:

cd X
find ./ -iname "*.xlsx" > /tmp/out
SAVEIFS=$IFS;
IFS=$(echo -en "\n\b");
while read line ;
do
   cd $(dirname $line);
   libreoffice --headless --convert-to xls $(basename $line);
   echo $PWD;
   cd -;
done < /tmp/out;
IFS=$SAVEIFS
| improve this answer | |
  • The fiddling with $IFS was a new thing for me. Thanks for the excellent question! :) – Nehal J Wani May 15 '14 at 16:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.