Unix & Linux
linux conversion spreadsheet
Updated Mon, 05 Sep 2022 02:06:31 GMT

Converting multiple .xls format files to .xlsx format


I am new to Linux. I know that there are ways to convert between file formats in Linux through the terminal. But is there any way I achieve the following?

I have 3 folders, inside those folders, there are 15 more folders. Each of those folders has 12 files in .xls format, and some other .R files and pdf files. Is there a way to create an exact duplicate of these 3 folders, inside which all the .xls are converted to .xlsx format?

If the following seems too unsubstantial, what is the way to just convert all the .xls files in one folder into .xlsx file format? Then I can replicate the aforesaid.

note: I don't have LibreOffice, I use WPS. If the following requires LibreOffice I can install it if necessary.




Solution

It will require libreoffice

From this Stack Overflow question Unix command to convert xls file into xlsx file? :

libreoffice --convert-to xlsx my.xls --headless

Note that the --convert-to option implies the --headless option (it did not in Libreoffice version 4.2.3 when the SO answer above was written in 2014, but has done so since at least Libreoffice 6 in 2018, and probably much longer than that). It doesn't hurt to add it to the command line, but it isn't required.

Then you can integrate it into a find command :

find . -type f -name "*.xls" -exec libreoffice --convert-to xlsx {} \;

This command will look for files, in the current directory, with a name ending with .xls and execute the command replacing {} by the filename.

man libreoffice states :

--convert-to output_file_extension[:output_filter_name] [--outdir output_dir] file...

Batch converts files. If --outdir is not specified then the current working directory is used as the output directory for the converted files. It implies --headless

Examples:

 --convert-to pdf *.doc

Converts all .doc files to PDFs.

 --convert-to pdf:writer_pdf_Export --outdir /home/user *.doc

Converts all .doc files to PDFs using the settings in the Writer PDF export dialog and saving them in /home/user.

From @cas in the comments,

libreoffice can take multiple filename arguments (the examples from the man page show this) so it would be faster to run as find . -type f -name '*.xls' -exec libreoffice --convert-to xlsx {} + - only needing to run libreoffice once instead of once per file

find . -type f -name '*.xls' -exec libreoffice --convert-to xlsx {} +




Comments (1)

  • +1 – +1. BTW, libreoffice can take multiple filename arguments (the examples from the man page show this) so it would be faster to run as find . -type f -name '*.xls' -exec libreoffice --convert-to xlsx {} + - only needing to run libreoffice once instead of once per file — Jul 19, 2022 at 14:07