1

XlsxWriter在我的 Python3.3 应用程序中使用,当打开 excel 文件并运行 py 脚本时,发生了这个错误,我无法处理 except except PermissionError

错误:

Exception PermissionError: PermissionError(13, 'Permission denied') in <bound method Workbook.__del__ of <xlsxwriter.workbook.Workbook object at 0x00000000032C3400>> ignored

我如何通过尝试处理此错误?

4

2 回答 2

1

您只需要在 周围添加 try/exceptclose()以处理文件已打开并被 Excel 锁定的情况。

try:
    workbook.close()
except:
    # Handle your exception here.
    print("Couldn't create xlsx file")

如果需要,您可以为特定条件添加特定的异常处理程序。

于 2014-02-05T12:51:54.833 回答
1
import xlsxwriter

try:
    # Create a workbook and add a worksheet.
    workbook = xlsxwriter.Workbook('Expenses01.xlsx')
    worksheet = workbook.add_worksheet()

    # Some data we want to write to the worksheet.
    expenses = (
        ['Rent', 1000],
        ['Gas',   100],
        ['Food',  300],
        ['Gym',    50],
    )

    # Start from the first cell. Rows and columns are zero indexed.
    row = 0
    col = 0

    # Iterate over the data and write it out row by row.
    for item, cost in (expenses):
        worksheet.write(row, col,     item)
        worksheet.write(row, col + 1, cost)
        row += 1

    # Write a total using a formula.
    worksheet.write(row, 0, 'Total')
    worksheet.write(row, 1, '=SUM(B1:B4)')

    workbook.close()
except:
    #
    #DO WHAT YOU WANT TO DO

尝试这个。

于 2014-02-05T07:54:46.057 回答