如何使用python将多个xlsx工作表转换为csv

时间:2015-11-30 09:40:18

标签: python xlrd

在单个Excel工作表的情况下,我能够将xlsx转换为csv。
如果在单个excel文件中有多个工作表,我该怎么做呢? 我试过了:

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

1 个答案:

答案 0 :(得分:-1)

试试这个

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