bash R Linux Shell批量将多表xls转换为csv

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15178218/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 04:43:52  来源:igfitidea点击:

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

rbashcsvxls

提问by Henk

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

在 R 中,我有一个脚本获取多个 xls 文件的内容<循环目录以获取 Excel 内容>。

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.

所有文件大约为 2 MB。该脚本需要几秒钟来处理 3 个文件,但现在在 Debian i7 系统上运行了 6 个小时,但没有对 120 个文件产生任何结果。

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>:

因此,更好的解决方案是 [希望] 使用 ssconvert 将所有 xls 文件转换为 csv,使用 bash 脚本 < 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'].

这个脚本完成了这项工作,但是我的内容在第 14 页,而这个脚本生成的 csv 文件只返回第一个工作表 [我用 'ssconvert' 替换了 'xls2csv']。

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

可以采用此脚本来仅拾取工作簿中的第 14 页吗?

采纳答案by agstudy

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.

要查看所有 xls2csv 详细信息,请参见此处

EDIT

编辑

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

OP 找到了正确的答案,所以我编辑了我的答案以添加它:

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

回答by Theodore Lytras

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().

对于这项工作,我使用了一个名为 ssconverter.py 的 Python 脚本(您可以在此处找到,向下滚动并下载两个附件,ssconverter.py 和 ooutils.py),我使用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.

您需要安装 python 和 python-uno。