Xls to csv converter

0 votes

My.xlsx and.xls files are being converted to.csv using win32.client in Python. This code is giving me an error when I run it. My code is:

def convertXLS2CSV(aFile):
    '''converts a MS Excel file to csv w/ the same name in the same directory'''

    print "------ beginning to convert XLS to CSV ------"

    try:
        import win32com.client, os
        from win32com.client import constants as c
        excel = win32com.client.Dispatch('Excel.Application')

        fileDir, fileName = os.path.split(aFile)
        nameOnly = os.path.splitext(fileName)
        newName = nameOnly[0] + ".csv"
        outCSV = os.path.join(fileDir, newName)
        workbook = excel.Workbooks.Open(aFile)
        workbook.SaveAs(outCSV, c.xlCSVMSDOS) # 24 represents xlCSVMSDOS
        workbook.Close(False)
        excel.Quit()
        del excel

        print "...Converted " + nameOnly + " to CSV"
    except:
        print ">>>>>>> FAILED to convert " + aFile + " to CSV!"

convertXLS2CSV("G:\\hello.xlsx")

I am not able to find the error in this code. Please help.

Oct 21, 2022 in Others by Kithuzzz
• 38,010 points
503 views

1 answer to this question.

0 votes

Use xlrd - it's faster, cross-platform, and works directly with the file.

As of version 0.8.0, xlrd reads both XLS and XLSX files.

But as of version 2.0.0, support was reduced back to only XLS.

import xlrd
import csv

def csv_from_excel():
    wb = xlrd.open_workbook('your_workbook.xls')
    sh = wb.sheet_by_name('Sheet1')
    your_csv_file = open('your_csv_file.csv', 'wb')
    wr = csv.writer(your_csv_file, quoting=csv.QUOTE_ALL)

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

    your_csv_file.close()
answered Oct 22, 2022 by narikkadan
• 63,420 points

Related Questions In Others

0 votes
1 answer

csv to excel conversion

So Basically u have to follow few ...READ MORE

answered Feb 9, 2022 in Others by gaurav
• 23,260 points
429 views
0 votes
1 answer

Convert csv- or Excel-file (xlsx) to kml with custom markers

With some help from the community, I ...READ MORE

answered Sep 26, 2022 in Others by narikkadan
• 63,420 points
853 views
0 votes
1 answer

I want to include a pdf to excel converter in my template?

You're app hosted locally only?  But even ...READ MORE

answered Oct 2, 2022 in Others by narikkadan
• 63,420 points
369 views
0 votes
1 answer

Converting .pdf files to excel (.xls)

It is not always possible and typically ...READ MORE

answered Oct 30, 2022 in Others by narikkadan
• 63,420 points
521 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,023 views
0 votes
1 answer
0 votes
1 answer

Convert xls to csv without any data changes

Here are the 3 solutions I have ...READ MORE

answered Dec 16, 2022 in Others by narikkadan
• 63,420 points
638 views
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP