python-异常(‘工作簿析构函数中捕获了异常.工作簿可能需要显式close().’
作者:互联网
在Oracle Linux 6.6中运行Python程序时,出现以下异常.
我正在使用Python版本2.6.
Exception(‘Exception caught in workbook destructor. Explicit close()
may be required for workbook.’
我的代码是:
import warnings
warnings.filterwarnings("ignore")
import mysql.connector
import smtplib
import math
#from xlrd import open_workbook
import numbers, string
import xlsxwriter
import string
import time
import base64
import subprocess
import os
import sys
import commands
import itertools
#import xlwt
import datetime as dt
import email.mime.application
from datetime import datetime
from datetime import datetime,timedelta, date
from email import Encoders
from tempfile import TemporaryFile
#from xlwt import Workbook, XFStyle, Borders, Pattern, Font
from email.MIMEMultipart import MIMEMultipart
from email.mime.audio import MIMEAudio
from email.mime.base import MIMEBase
from email.mime.image import MIMEImage
from email.MIMEText import MIMEText
from email.Utils import formatdate
import datetime
from xlsxwriter.workbook import Workbook
from xlsxwriter import Workbook
curtme = datetime.datetime.strftime(datetime.datetime.now(), '%Y-%m-%d_%H:%M:%S')
workbook = xlsxwriter.Workbook('/Export/Report_'+curtme+'.xlsx')
worksheet = workbook.add_worksheet()
completed_fmt = workbook.add_format({'bold': True, 'border': 6,'align': 'center','valign': 'vcenter','bg_color': 'yellow','font_size':'12', 'font_name':'Calibri'})
worksheet.set_column(0,9,36)
worksheet.merge_range('A2:C6',None, None)
worksheet.merge_range('A2:C6',None, None)
cellformat = workbook.add_format({'bold': True, 'border': 6,'align': 'center','valign': 'vcenter','font_size':'16', 'font_name':'Calibri'})
cellfmt = workbook.add_format({'bold': False, 'border': 6,'align': 'center','valign': 'vcenter','font_size':'12', 'font_name':'Calibri'})
worksheet.insert_image('A1', 'Image/logo.jpg')
worksheet.merge_range('B1:C6', "Report", cellformat)
rowx = 6
for colx, value in enumerate(headings):
worksheet.write(rowx, colx, value, completed_fmt)
i=1
for i, row in enumerate(myresult):
for j, col in enumerate(row):
worksheet.write(i+7, j, col,cellfmt)
workbook.close()
db.close()
解决方法:
异常表明可能需要close().这是为了捕获xlsxwriter异常(其中隐式关闭不足).
由于close()已包含在您的程序中,因此不是异常的来源.
实际错误将在堆栈跟踪中位于其上方.一些潜在的问题:
>缩进是错误的.
>标题未定义.
>未定义myresult.
标签:xlsxwriter,python 来源: https://codeday.me/bug/20191120/2043647.html