How do I export all sheets from a spreadsheet?

I have a bunch of .xls and .xlsx files

Each of them contains lots of sheets (tabs)

I’d like to use LibreOffice headless (or UnoConv maybe ?) to programmatically export EVERY sheet in csv format

Currently I am using a hack written in Python.

1 Like

I’ve opened an enhancement bug report[¹] to have this feature!

[¹] https://bugs.documentfoundation.org/show_bug.cgi?id=100815

Ah, what the hell why the questions get closed??? I can imagine it’s useful to be answered, but closed? I wanted to add an answer, which is still an answer even after 6 years, that it’s implemented in tdf#135762 for 7.2, but I will not reopen issues closed by others, who obviously know better.

The solution is to iterate through the available sheets, making each active in sequence, and for each execute the storeToURL method while applying the filter "Text - txt - csv (StarCalc)". The following code example does this. Edit the file path to conform to your own situation:

REM  *****  BASIC  *****

Sub convertSheetsToCSVs
Dim fileProps(0) as new com.sun.star.beans.PropertyValue
sheets = ThisComponent.Sheets

fileProps(0).Name = "FilterName"
fileProps(0).Value = "Text - txt - csv (StarCalc)"

i = 0

Do While sheets.Count > i
  sheet = sheets.getByIndex(i)
  cntrllr = ThisComponent.CurrentController
  cntrllr.setActiveSheet(sheet)
  sURL = "file:///home/doug/Documents/test_macro" & sheets.ElementNames(i) & ".csv"
  ThisComponent.storeToURL(sURL, fileProps())
  i = i + 1
Loop
End Sub

(if this answers your question please accept the answer by clicking the check mark (image description) to the left)

In @doug 's answer, be sure to set a location in your machine. To save output in your Documents folder:

sURL= "file:///home/your-machine-name/Documents/" & sheets.ElementNames(i) & ".csv"

I used this: https://www.briankoponen.com/libreoffice-export-sheets-csv/ and it worked fine

As I came across this again…
LibreOffice 7.2 introduced a --convert-to csv:... parameter to specify which sheet (or all) to export, see LibreOffice 7.2 Community: Release Notes - The Document Foundation Wiki

1 Like