如何读取ODS文档(例如Libreoffice计算)并在Julia DataFrame中转换



如何从julia dataframe中从ODS电子表格导入数据(由OpenOffice,libreoffice,...使用)?

(这是一个社区Wiki Q& a)

如果在计算机上安装了python,朱莉娅(Julia)可以很简单地使用ezodf模块,使用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)

(这只是此答案中Davidovitch的Python代码的Julia的重写)

编辑:

我现在根据此代码写了一个朱莉娅包:odsio。

它提供了多种功能来从ODS文件(包括单元格范围)导入数据,并希望很快也将允许导出。

edit2:

由于版本v0.1.0

,现在支持将其导出到ODS

最新更新