4

How do I convert to CSV anything else than the first sheet of an excel file to CSV?

My current code is :

libreoffice --headless --convert-to csv --outdir data/csv_files  data/excel_files/*.*

I would like to convert the n-th sheet, or even all sheets, not only the first one.

Thx!

2 Answers 2

2

When saving as CSV, LibreOffice will only save the active sheet. The command
libreoffice --convert-to will use the first sheet.

Instead of using --convert-to, this is a relatively simple task with macros. The macro just needs to activate a particular sheet and then save as CSV. If you write a macro called "MyMacro1" that takes the name of a sheet, it can be run from the command line like this:

soffice macro:///Standard.Module1.MyMacro1("sheet 2")

Some example code is here. Also keep Andrew Pitonyak's Macro document handy as a reference.

Alternatively, here is a solution using xslx2csv, which I have not tried: https://ask.libreoffice.org/en/question/46466/how-to-convert-specific-sheet-to-csv-via-command-line/.

6
  • I haven't worked with libreoffice at all from command line. Where do I write / put the said macro?
    – Catalin
    Commented Mar 15, 2016 at 8:22
  • Open LibreOffice and go to Tools -> Macros -> Organize Macros -> LibreOffice Basic. Expand My Macros / Standard and select Module1. Add the macro here, which will put it in the user profile directory on your machine.
    – Jim K
    Commented Mar 15, 2016 at 14:00
  • 4
    Can this be done from terminal, without the use of LibreOffice UI?
    – Catalin
    Commented Mar 15, 2016 at 18:10
  • Are you asking how to write the macro using the terminal? If so, you may be interested in writing in python, using a text editor such as vi. Then copy the .py file to the user profile directory under Scripts/python. Voilà -- no graphical UI involved!
    – Jim K
    Commented Mar 16, 2016 at 1:23
  • What Python xls parser library did you use for the above?
    – JosephK
    Commented Jul 7, 2016 at 14:18
2

There is a new option now on the CLI:

https://wiki.documentfoundation.org/ReleaseNotes/7.2#Document_Conversion

soffice --convert-to csv:"Text - txt - csv (StarCalc)":44,34,UTF8,1,,0,false,true,false,false,false,-1 sample.ods --outdir data/csv_files

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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