16

I need to convert ".xlsx" file to ".xls" using shell command.

At my work we are currently using xlsx2csv command but now requirement has been changed and we need to convert all ".xlsx" files to ".xls" files for further calculation.

For that, Some guy at my work has developed one command that can convert ".xlsx" to ".xls" but, that is applicable for only one sheet..

We have multiple sheets in one file.

Thanks in advance....

Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
Prem Joshi
  • 437
  • 1
  • 3
  • 10
  • It can also be done using the pyxl library, this does the opposite: http://stackoverflow.com/questions/9918646/how-to-convert-xls-to-xlsx – moo Mar 31 '17 at 10:53

2 Answers2

22

If you install LibreOffice, you can use the following command:

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

or just:

libreoffice --convert-to xls myfile.xlsx

in recent version (>= 4.5) where --convert-to implies --headless.

This will create myfile.xls, and keep the original myfile.xlsx—so you’ll probably need to do a cleanup after you've validated the conversion is successful.

lgeorget
  • 13,656
  • 2
  • 41
  • 63
forquare
  • 3,396
  • 5
  • 21
  • 32
  • This option is very good, but i think there is one problem, it opens libreoffice instance and then converts it to ".xls" file. Is there any other option that will not open libreoffice and still able to convert to ".xls" file ? can you help me with this ? @forquare – Prem Joshi Mar 27 '17 at 08:03
  • I'm using the command on FreeBSD so maybe slightly different, but on my computer the LibreOffice GUI appeared for about 5 seconds the first time, and then not at all subsequent times. Are you saying that LibreOffice GUI opens and stays open every time? – forquare Mar 27 '17 at 08:06
  • 1
    @PremJoshi Man page says: "[--convert-to] implies --headless." so the GUI is not opened. However, when you launch it for the first time after boot, it may show a splash screen or something like that while it registers services (depending on your desktop). – lgeorget Mar 27 '17 at 08:09
  • yes ! it stays open ! We have to use this in out script and we can not open libreoffice every time ! we are scheduling this script at mid night so there will be no one to close libreoffice manually. – Prem Joshi Mar 27 '17 at 08:09
  • @PremJoshi could you add some details about which Linux distro you are using, which Desktop Environment? – forquare Mar 27 '17 at 08:12
  • What version of libreoffice do you use, on which system? It's probably a difference between versions. – lgeorget Mar 27 '17 at 08:12
  • Actually we are using remote servers, 1) CentOS 6.8 and 2) LibreOffice 4.3.7.2 ! Is there any working difference between what @forquare is saying and what I'm actually getting ? – Prem Joshi Mar 27 '17 at 08:15
  • 1
    @Igeorget ! option "--headless" worked for me ! this was exact answer for my question. – Prem Joshi Mar 27 '17 at 08:17
  • I am using LibreOffice v5.2.5, and Xfce 4.12. – forquare Mar 27 '17 at 08:18
  • 3
    The commit that made `--convert-to` imply `--headless` is https://gerrit.libreoffice.org/gitweb?p=core.git;a=commit;h=fe5527f1c6dc259af464a0593e86a2da2983b7ed (version 4.4). In previous versions, both options have to be passed. – lgeorget Mar 27 '17 at 08:51
  • (+1). Is this equivalent to "good old" `unoconv -f xls f.xlsx` ? – JJoao Mar 27 '17 at 09:42
  • 1
    Be aware of the xls format 65535 row limit, as this command does not print any warning message if the xlsx file exceeds that limit (it will output a 65535 row file which does not correspond to the original, with no warning) – golimar Aug 26 '19 at 13:31
7

you can also try ssconvert the companion converter tool of gnumeric

ssconvert in.xlsx out.xls

(as usually, see man ssconvert)

For normal cases, this is fine. If the input file is very advanced in both (libreoffice and gnumeric)-based converters, some details may be lost.

JJoao
  • 11,887
  • 1
  • 22
  • 44