Convert .xls/.xlsx spreadsheets to multiple .csv's based on a list
You can just put a loop inside another loop.
To avoid errors, don't use for
with find
results.
while IFS= read -r file; do
while IFS= read -r sheet; do
in2csv --sheet "$sheet" "$file" > "${file%.*}-${sheet}.csv"
done < <(in2csv -n "$file")
done < <(find . -name '*.xls' -o -name '*.xlsx')
Skipping find and using bash:
shopt -s globstar # enable recursive globbing
for f in **/*.xls{,x} # for files ending in .xls or .xlsx
do
in2csv -n "$f" | # get the sheetnames
xargs -I {} bash -c 'in2csv --sheet "$2" "$1" > "${1%.*}"-"$2".csv' _ "$f" {} # {} will be replaced with the sheetname
done
csvkit version > 1.0.2 has a builtin function to write all sheets:
--write-sheets: WRITE_SHEETS
The names of the Excel sheets to write to files, or
"-" to write all sheets.
So you could try the following:
find . -name '*.xls' -o -name '*.xlsx' -exec in2csv --write-sheets "-" {} \;
Note:
This seems not to work 100% as expected. But worth a try and as this is the first version with that option maybe in future versions the implementation is better/easier.