0

How to import data from a ODS spreadsheet (used by OpenOffice, LibreOffice,...) in a Julia DataFrame ?

(this is a community wiki Q&A)

Antonello
  • 6,092
  • 3
  • 31
  • 56

1 Answers1

5

If python is installed on the machine, the ezodf module can be used pretty straightforwardly by Julia, using PyCall:

using PyCall
using DataFrames

@pyimport ezodf
doc = ezodf.opendoc("test.ods")
nsheets = length(doc[:sheets])
println("Spreadsheet contains $nsheets sheet(s).")
for sheet in doc[:sheets]
    println("---------")
    println("   Sheet name : $(sheet[:name])")
    println("Size of Sheet : (rows=$(sheet[:nrows]()), cols=$(sheet[:ncols]()))")
end

# convert the first sheet to a dictionary
sheet = doc[:sheets][1]
df_dict = Dict()
col_index = Dict()
for (i, row) in enumerate(sheet[:rows]())
  # row is a list of cells
  # assume the header is on the first row
  if i == 1
      # columns as lists in a dictionary
      [df_dict[cell[:value]] = [] for cell in row]
      # create index for the column headers
      [col_index[j]=cell[:value]  for (j, cell) in enumerate(row)]
      continue
  end
  for (j, cell) in enumerate(row)
      # use header instead of column index
      append!(df_dict[col_index[j]],cell[:value])
  end
end  

# and convert the dictionary to a DataFrame
df = DataFrame(df_dict)

(this is just a rewriting in Julia of davidovitch's python code on this answer)

EDIT:

I did now wrote, based on this code, a Julia Package: OdsIO.

It provides several functions to import data from ods files (including cell range) and hopefully soon it will allow export as well.

EDIT2:

Export to Ods is now supported since version v0.1.0

Antonello
  • 6,092
  • 3
  • 31
  • 56