how to convert xls to xlsx

前端 未结 14 1306
生来不讨喜
生来不讨喜 2020-11-27 03:58

I have some *.xls(excel 2003) files, and I want to convert those files into xlsx(excel 2007).

I use the uno python package, when I save the documents, I can set the

14条回答
  •  爱一瞬间的悲伤
    2020-11-27 04:48

    The answer by Ray helped me a lot, but for those who search a simple way to convert all the sheets from a xls to a xlsx, I made this Gist:

    import xlrd
    from openpyxl.workbook import Workbook as openpyxlWorkbook
    
    # content is a string containing the file. For example the result of an http.request(url).
    # You can also use a filepath by calling "xlrd.open_workbook(filepath)".
    
    xlsBook = xlrd.open_workbook(file_contents=content)
    workbook = openpyxlWorkbook()
    
    for i in xrange(0, xlsBook.nsheets):
        xlsSheet = xlsBook.sheet_by_index(i)
        sheet = workbook.active if i == 0 else workbook.create_sheet()
        sheet.title = xlsSheet.name
    
        for row in xrange(0, xlsSheet.nrows):
            for col in xrange(0, xlsSheet.ncols):
                sheet.cell(row=row, column=col).value = xlsSheet.cell_value(row, col)
    
    # The new xlsx file is in "workbook", without iterators (iter_rows).
    # For iteration, use "for row in worksheet.rows:".
    # For range iteration, use "for row in worksheet.range("{}:{}".format(startCell, endCell)):".
    

    You can find the xlrd lib here and the openpyxl here (you must download xlrd in your project for Google App Engine for example).

提交回复
热议问题