好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

在Python中如何将xls转换为xlsx

我有一些* .xls(excel 2003)文件,我想将这些文件转换为xlsx(excel 2007).

当我保存文档时,我使用uno python包, 我可以设置过滤器名称:MS Excel 97 但是没有像’MS Excel 2007’那样的过滤器名称,

请帮帮我,如何设置过滤器名称将xls转换为xlsx?

我以前必须这样做.主要思想是使用xlrd模块打开并解析xls文件并编写 使用openpyxl模块将内容内容添加到xlsx文件中.

这是我的代码.注意!它无法处理复杂的xls文件,如果要使用它,应该添加自己的解析逻辑.

import xlrd
from openpyxl.workbook import Workbook
from openpyxl.reader.excel import load_workbook, InvalidFileException

def open_xls_as_xlsx(filename):
    # first open using xlrd
    book = xlrd.open_workbook(filename)
    index = 0
    nrows, ncols = 0, 0
    while nrows * ncols == 0:
        sheet = book.sheet_by_index(index)
        nrows = sheet.nrows
        ncols = sheet.ncols
        index += 1

    # prepare a xlsx sheet
    book1 = Workbook()
    sheet1 = book1.get_active_sheet()

    for row in xrange(0, nrows):
        for col in xrange(0, ncols):
            sheet1.cell(row=row, column=col).value = sheet.cell_value(row, col)

    return book1

查看更多关于在Python中如何将xls转换为xlsx的详细内容...

  阅读:39次