def get_xls_export(self, context):
datas = self._get_datas(context)
output = io.BytesIO()
export_header = (
self.request.GET.get('export_xls_header', 'off') == 'on')
model_name = self.opts.verbose_name
book = xlwt.Workbook(encoding='utf8')
sheet = book.add_sheet(
u"%s %s" % (_(u'Sheet'), force_text(model_name)))
styles = {'datetime': xlwt.easyxf(num_format_str='yyyy-mm-dd hh:mm:ss'),
'date': xlwt.easyxf(num_format_str='yyyy-mm-dd'),
'time': xlwt.easyxf(num_format_str='hh:mm:ss'),
'header': xlwt.easyxf('font: name Times New Roman, color-index red, bold on', num_format_str='#,##0.00'),
'default': xlwt.Style.default_style}
if not export_header:
datas = datas[1:]
for rowx, row in enumerate(datas):
for colx, value in enumerate(row):
if export_header and rowx == 0:
cell_style = styles['header']
else:
if isinstance(value, datetime.datetime):
cell_style = styles['datetime']
elif isinstance(value, datetime.date):
cell_style = styles['date']
elif isinstance(value, datetime.time):
cell_style = styles['time']
else:
cell_style = styles['default']
sheet.write(rowx, colx, value, style=cell_style)
book.save(output)
output.seek(0)
return output.getvalue()
python类easyxf()的实例源码
def get_xls_export(self, context):
datas = self._get_datas(context)
output = io.BytesIO()
export_header = (
self.request.GET.get('export_xls_header', 'off') == 'on')
model_name = self.opts.verbose_name
book = xlwt.Workbook(encoding='utf8')
sheet = book.add_sheet(
u"%s %s" % (_(u'Sheet'), force_text(model_name)))
styles = {'datetime': xlwt.easyxf(num_format_str='yyyy-mm-dd hh:mm:ss'),
'date': xlwt.easyxf(num_format_str='yyyy-mm-dd'),
'time': xlwt.easyxf(num_format_str='hh:mm:ss'),
'header': xlwt.easyxf('font: name Times New Roman, color-index red, bold on', num_format_str='#,##0.00'),
'default': xlwt.Style.default_style}
if not export_header:
datas = datas[1:]
for rowx, row in enumerate(datas):
for colx, value in enumerate(row):
if export_header and rowx == 0:
cell_style = styles['header']
else:
if isinstance(value, datetime.datetime):
cell_style = styles['datetime']
elif isinstance(value, datetime.date):
cell_style = styles['date']
elif isinstance(value, datetime.time):
cell_style = styles['time']
else:
cell_style = styles['default']
sheet.write(rowx, colx, value, style=cell_style)
book.save(output)
output.seek(0)
return output.getvalue()
def createSpreadsheet():
wb = xlwt.Workbook()
sheet = wb.add_sheet("Google Alerts")
style = xlwt.easyxf('font: bold 1')
sheet.write(0, 3, 'Headline', style)
sheet.write(0, 1, 'Company', style)
sheet.write(0, 4, 'URL', style)
sheet.write(0, 0, 'Date', style)
cur_row = 1
for url in LA_HONDA_ALERTS_URLS:
print 'Processing google alerts for ' + LA_HONDA_ALERTS_URLS[url] + '...'
r = requests.get(url)
xml = r.text
soup = BeautifulSoup(xml)
for title, link, date in zip(soup.findAll('title')[1:], soup.findAll('link')[1:], soup.findAll('published')):
title = cleanTitle(title)
link = cleanLink(link)
date = cleanDate(date)
writeToSheet(sheet, title, LA_HONDA_ALERTS_URLS[url], link, date, cur_row)
cur_row = cur_row + 1
processSheet(sheet)
savewb(wb)
def get_xls_export(self, context):
datas = self._get_datas(context)
output = io.BytesIO()
export_header = (
self.request.GET.get('export_xls_header', 'off') == 'on')
model_name = self.opts.verbose_name
book = xlwt.Workbook(encoding='utf8')
sheet = book.add_sheet(
u"%s %s" % (_(u'Sheet'), force_text(model_name)))
styles = {'datetime': xlwt.easyxf(num_format_str='yyyy-mm-dd hh:mm:ss'),
'date': xlwt.easyxf(num_format_str='yyyy-mm-dd'),
'time': xlwt.easyxf(num_format_str='hh:mm:ss'),
'header': xlwt.easyxf('font: name Times New Roman, color-index red, bold on', num_format_str='#,##0.00'),
'default': xlwt.Style.default_style}
if not export_header:
datas = datas[1:]
for rowx, row in enumerate(datas):
for colx, value in enumerate(row):
if export_header and rowx == 0:
cell_style = styles['header']
else:
if isinstance(value, datetime.datetime):
cell_style = styles['datetime']
elif isinstance(value, datetime.date):
cell_style = styles['date']
elif isinstance(value, datetime.time):
cell_style = styles['time']
else:
cell_style = styles['default']
sheet.write(rowx, colx, value, style=cell_style)
book.save(output)
output.seek(0)
return output.getvalue()
def wb_update(wr_ws, status, i):
# build green and red style sheets for excel
green_st = xlwt.easyxf('pattern: pattern solid;')
green_st.pattern.pattern_fore_colour = 3
red_st = xlwt.easyxf('pattern: pattern solid;')
red_st.pattern.pattern_fore_colour = 2
yellow_st = xlwt.easyxf('pattern: pattern solid;')
yellow_st.pattern.pattern_fore_colour = 5
# if stanzas to catch the status code from the request
# and then input the appropriate information in the workbook
# this then writes the changes to the doc
if status == 200:
wr_ws.write(i, 1, 'Success (200)', green_st)
if status == 400:
print("Error 400 - Bad Request - ABORT!")
print("Probably have a bad URL or payload")
wr_ws.write(i, 1, 'Bad Request (400)', red_st)
pass
if status == 401:
print("Error 401 - Unauthorized - ABORT!")
print("Probably have incorrect credentials")
wr_ws.write(i, 1, 'Unauthorized (401)', red_st)
pass
if status == 403:
print("Error 403 - Forbidden - ABORT!")
print("Server refuses to handle your request")
wr_ws.write(i, 1, 'Forbidden (403)', red_st)
pass
if status == 404:
print("Error 404 - Not Found - ABORT!")
print("Seems like you're trying to POST to a page that doesn't"
" exist.")
wr_ws.write(i, 1, 'Not Found (400)', red_st)
pass
if status == 666:
print("Error - Something failed!")
print("The POST failed, see stdout for the exception.")
wr_ws.write(i, 1, 'Unkown Failure', yellow_st)
pass
if status == 667:
print("Error - Invalid Input!")
print("Invalid integer or other input.")
wr_ws.write(i, 1, 'Unkown Failure', yellow_st)
pass