【问题标题】:Convert xls to xlsx without win32com在没有win32com的情况下将xls转换为xlsx
【发布时间】:2021-10-24 01:57:13
【问题描述】:

您需要在没有 win32com 的主机上将 xls 转换为 xlsx。我试过这段代码:

import xlrd, os
from openpyxl.workbook import Workbook


def open_xls_as_xlsx(filename):
    # first open using xlrd
    book = xlrd.open_workbook( filename =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.active()

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

    return book1


open_xls_as_xlsx(os.getcwd() + '/Расписание_1-4_курсов_с_01.04_по_05.07.2021_(2_поток).xls').save(filename = 'path_to_file.xlsx')

但出现错误:

'Worksheet' object is not callable

或者我可以做点别的吗?请帮忙

【问题讨论】:

  • 请edit您的问题并发布错误和回溯的全文。不要发布文字图片。

标签: python excel file converters


【解决方案1】:

激活后,不能有()。

 sheet1 = book1.active

此外,2d for 循环中存在错误。 xlrd 从 0 开始坐标,而 openpyxl 从 1 开始。

    for row in range(0, nrows):
        for col in range(0, ncols):
            print(sheet.cell_value(row,col))
            sheet1.cell(row=row+1, column=col+1).value = sheet.cell_value(row, col)

【讨论】:

    猜你喜欢
    • 2014-05-05
    • 1970-01-01
    • 2012-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-25
    • 2016-06-26
    相关资源
    最近更新 更多