R Linux Shell convert multi-sheet xls to csv in batch

不打扰是莪最后的温柔 提交于 2019-12-02 00:50:22

问题


In R i have a script gets content of multiple xls files <Loop over directory to get Excel content>.

All files are about 2 MB. The script takes a few seconds for 3 files, but is now running for 6 hours on a Debian i7 system without results on 120 files.

A better solution is therefore [hopefully] to convert all xls files to csv using ssconvert, using a bash script <Linux Shell Script For Each File in a Directory Grab the filename and execute a program>:

for f in *.xls ; do xls2csv "$f" "${f%.xls}.csv" ; done

This script does the job, however my content is in sheet nr 14, whereas the csv files produced by this script just return the first sheet [i replaced 'xls2csv' with 'ssconvert'].

Can this script be adopted to pickup only sheet nr 14 in the workbook?


回答1:


If you know the worksheet name, you can do this:

for f in *.xls ; xls2csv -x "$f" -w sheetName -c "${f%.xls}.csv";done

To see all the xls2csv details see here.

EDIT

The OP find the right answer, so I edit mine to add it :

for f in *.xls ; do xls2csv -x "$f" -f -n 14 -c "${f%.xls}.csv" 



回答2:


For this job I use a python script named ssconverter.py (which you can find here, scroll down and download the two attachments, ssconverter.py and ooutils.py), which I call directly from R using system().

It can extract a specific sheet in the workbook, not only by name but also by sheet number, for example:

ssconverter.py infile.xls:2 outfile.csv

to extract the second sheet.

You need to have python and python-uno installed.



来源:https://stackoverflow.com/questions/15178218/r-linux-shell-convert-multi-sheet-xls-to-csv-in-batch

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!