0

I`m trying to convert .xls to .csv but when i run the code below nothing happens.

import xlrd
import csv

def csv_from_excel():

    wb = xlrd.open_workbook('d://Documents and Settings//tdrub//Desktop//TreinamentoPython XLS-CSV//Teste.xls')
    sh = wb.sheet_by_name('Sheet1')
    Agencia = open('d://Documents and Settings//tdrub//Desktop//Agencia.csv', 'wb')
    wr = csv.writer(Agencia, quoting=csv.QUOTE_ALL)

    for rownum in xrange(sh.nrows):
         wr.writerow(sh.row_values(rownum))

    Agencia.close()

The directory is correct, the sheet name is correct but when i run the code no .csv file is created.

I appreciate if someone can help me :)

Tadeu Drub
  • 11
  • 3
  • May be a stupid question, but are you actually executing this function or just running the code? – 4d4c Nov 07 '13 at 10:44
  • Hmm have a look at Andi's solution(scroll down) http://stackoverflow.com/questions/9884353/xls-to-csv-convertor – LotusUNSW Nov 07 '13 at 10:50
  • 1
    @ton1c Thanks, your question helped me a lot hahahaha, the biggest problem was the directory where my file was allocated, i changed to C: and now is working. – Tadeu Drub Nov 07 '13 at 11:12

1 Answers1

0

import xlrd
import csv
import os

file= open('out.csv', 'wb')
wr = csv.writer(file, quoting=csv.QUOTE_ALL)
book=xlrd.open_workbook("F.xls")
sheet=book.sheet_by_index(0)
for sheet in book.sheets():

for row in range(sheet.nrows):

wr.writerow(sheet.row_values(row))

Akshay
  • 1