如何使用 python 将多个 xlsx sheet 转换为 csv

How to convert multiple xlsx sheet to csv using python

在单个 excel sheet 的情况下,我可以将 xlsx 转换为 csv。
如果单个 excel 文件中有多个 sheet,我该如何做同样的事情?
我试过了:

workBook = xlrd.open_workbook(filePath)
sheet_names = workBook.sheet_names()
lenth = len(sheet_names)
for i in range(0,lenth):
   sheet =  workBook.sheet_by_name(sheet_names[i])
   yourcsvFile = open(csvPath, 'wb')
   wr = csv.writer(yourcsvFile, quoting=csv.QUOTE_ALL)
   for rownum in xrange(sheet.nrows):
         wr.writerow(sheet.row_values(rownum))
yourcsvFile.close()

试试这个

import sys
import xlrd
import csv
filePath = sys.argv[1] # user input file
csvPath = sys.argv[2]
workBook = xlrd.open_workbook(filePath)
sheet_names = workBook.sheet_names()
list_sheet = []
lenth = len(sheet_names)
for i in range(0,lenth):
   sheet =  workBook.sheet_by_name(sheet_names[i])
   list_sheet.append(sheet)
yourcsvFile = open(csvPath, 'wb')
wr = csv.writer(yourcsvFile, quoting=csv.QUOTE_ALL)
total_row = list_sheet[0].ncols
for k in xrange(0,1):
    for rownum in xrange(list_sheet[k].nrows):
      wr.writerow(list_sheet[k].row_values(rownum))
if len(sheet_names) > 1:
   for k in xrange(1,len(list_sheet)):
      if list_sheet[k].ncols != total_row:
         continue
    for rownum in xrange(1,list_sheet[k].nrows):
       wr.writerow(list_sheet[k].row_values(rownum))

yourcsvFile.close()