在Oracle Linux 6.6中运行我的Python程序时,我得到了以下异常。 我使用的是Python 2.6版。
异常('在工作簿析构函数中捕获异常。显式关闭() 可能需要工作簿。'
我的代码是:
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()
答案 0 :(得分:2)
例外情况表明close()
可能需要。这是为了捕获xlsxwriter异常,其中隐式关闭是不够的。
由于close()
已经包含在您的程序中,而不是异常的来源。
堆栈跟踪中的实际错误将高于它。一些潜在的问题:
headings
未定义。myresult
未定义。答案 1 :(得分:1)
您在执行代码时打开了文件'/Export/Report_'+curtme+'.xlsx',因此无法写入文件,因为它已经打开