xlwt 模块,Formula() 实例源码
我们从Python开源项目中,提取了以下9个代码示例,用于说明如何使用xlwt.Formula()。
defcreate_simple_xls(self,**kw):font0=xlwt.Font()font0.name='Times New Roman'font0.colour_index=2font0.bold=Truestyle0=xlwt.XFStyle()style0.font=font0style1=xlwt.XFStyle()style1.num_format_str='D-MMM-YY'wb=xlwt.Workbook(**kw)ws=wb.add_sheet('A Test Sheet')ws.write(0,0,'Test',style0)ws.write(1,0,datetime(2010,12,5),style1)ws.write(2,0,1)ws.write(2,1,1)ws.write(2,2,xlwt.Formula("A3+B3"))returnwb,ws
def_create_skipped_reason_summary(self):"""Create Skipped tests summary. Describe amount of tests skipped since they were filtered by tags, or noted as 'Already Passed' in the DB. """forskip_reasonin(self.TAGS_SKIP_MESSAGE,):self._write_to_cell(self.row_number,self.SUMMARY_RESULT_TYPE_COLUMN,self.DEFAULT_CELL_STYLE,self.SKIPPED_SUMMARY_PATTERN%skip_reason)skip_count_formula=xlwt.Formula(self.FORMULA_PATTERN%(self.TRACEBACK_COLUMN,self.TRACEBACK_COLUMN,self.row_number,skip_reason))self._write_to_cell(self.row_number,self.SUMMARY_RESULT_COUNTER_COLUMN,self.DEFAULT_CELL_STYLE,skip_count_formula)self.row_number+=1
def_create_result_summary(self):"""Create result summary at the end of the Excel report."""self.row_number+=self.ROWS_TO_SKIPforresult_typeinself.CONTENT_TO_STYLE:self._write_to_cell(self.row_number,self.SUMMARY_RESULT_TYPE_COLUMN,self.CONTENT_TO_STYLE[result_type],result_type)result_type_count=xlwt.Formula(self.FORMULA_PATTERN%(self.RESULT_COLUMN,self.RESULT_COLUMN,self.row_number,result_type))self._write_to_cell(self.row_number,self.SUMMARY_RESULT_COUNTER_COLUMN,self.DEFAULT_CELL_STYLE,result_type_count)self.row_number+=1ifresult_type==self.SKIPPED:self._create_skipped_reason_summary()
defcreate_simple_xls(self,**kw):font0=xlwt.Font()font0.name='Times New Roman'font0.colour_index=2font0.bold=Truestyle0=xlwt.XFStyle()style0.font=font0style1=xlwt.XFStyle()style1.num_format_str='D-MMM-YY'wb=xlwt.Workbook(**kw)ws=wb.add_sheet('A Test Sheet')ws.write(0,0,'Test',style0)ws.write(1,0,datetime(2010,12,5),style1)ws.write(2,0,1)ws.write(2,1,1)ws.write(2,2,xlwt.Formula("A3+B3"))returnwb,ws
deftest_intersheets_ref(self):book=xlwt.Workbook()sheet_a=book.add_sheet('A')sheet_a.write(0,0,'A1')sheet_a.write(0,1,'A2')sheet_b=book.add_sheet('B')sheet_b.write(0,0,xlwt.Formula("'A'!$A$1&'A'!$A$2"))out=BytesIO()book.save(out)
defdo_fill_workbook(self,workbook):"""export to excel all articles tin stock"""sheet=workbook.add_sheet(_(u"Stock"))line=0columns=[_(u'Id'),_(u'Name'),_(u'Category'),_(u'Purchase price'),_(u"Stock count"),_(u'Stock threshold'),_(u'Value'),]forcol,labelinenumerate(columns):self.write_cell(sheet,0,col,label,style=self.get_header_style())forline,iteminenumerate(self.get_store_items()):sheet.write(line+1,0,item.id)sheet.write(line+1,1,item.name)sheet.write(line+1,2,u'{0}'.format(item.category)ifitem.categoryelse'')sheet.write(line+1,3,item.purchase_priceifitem.purchase_priceelse0)sheet.write(line+1,4,''ifitem.stock_countisNoneelseitem.stock_count)sheet.write(line+1,5,''ifitem.stock_countisNoneelseitem.stock_threshold)sheet.write(line+1,6,xlwt.Formula('D{0}*E{0})'.format(line+2)))sheet.write(line+3,6,xlwt.Formula('SUM(G1:G{0})'.format(line+2)))
deftest_intersheets_ref(self):book=xlwt.Workbook()sheet_a=book.add_sheet('A')sheet_a.write(0,0,'A1')sheet_a.write(0,1,'A2')sheet_b=book.add_sheet('B')sheet_b.write(0,0,xlwt.Formula("'A'!$A$1&'A'!$A$2"))out=BytesIO()book.save(out)
defexternal_review_payment(request):checklists=Checklist.objects.filter(blueprint__slug='external_review').exclude(status__in=['new','dropped']).filter(Q(billing_state=None)|Q(billing_state__isnull=False,billing_state__billed_at=None))price=Price.objects.get_review_price()ifrequest.method=='POST':selected_for_payment=[]forchecklistinchecklists:ifrequest.POST.get('pay_%s'%checklist.pk,False):selected_for_payment.append(checklist)reviewers=User.objects.filter(pk__in=[c.user.pkforcinselected_for_payment]).distinct()xls=SimpleXLS()xls.write_row(0,(_('amt.'),_('reviewer'),_('EC-Nr.'),_('sum'),_('IBAN'),_('SWIFT-BIC'),_('Social Security Number')),header=True)fori,reviewerinenumerate(reviewers):checklists=reviewer.checklist_set.filter(pk__in=[c.pkforcinselected_for_payment])xls.write_row(i+1,[len(checklists),str(reviewer),", ".join(c.submission.get_ec_number_display()forcinchecklists),len(checklists)*price.price,reviewer.profile.iban,reviewer.profile.swift_bic,reviewer.profile.social_security_number,])r=len(reviewers)+1xls.write_row(r,[xlwt.Formula('SUM(A2:A%s)'%r),"","",xlwt.Formula('SUM(D2:D%s)'%r),])xls_buf=BytesIO()xls.save(xls_buf)doc=Document.objects.create_from_buffer(xls_buf.getvalue(),mimetype='application/vnd.ms-excel',doctype='checklist_payment')forchecklistinselected_for_payment:ChecklistBillingState.objects.update_or_create(checklist=checklist,defaults={'billed_at':doc.date})payment=ChecklistPayment.objects.create(document=doc)payment.checklists=selected_for_paymentreturnredirect('ecs.billing.views.view_checklist_payment',payment_pk=payment.pk)returnrender(request,'billing/external_review.html',{'checklists':checklists,'price':price,})
defwrite_model_value(self,mod_name,loop,dstReport):#print 'Fuction write_model_value'row=self.start_module_row+self.mod_row_offsetkeyword=Noneforxinrange(0,9):Hylink_Flag=Falseifx==0:keyword=self.dstRst_total_mod['DoneNum']elifx==1:keyword=dstReport['ScriptName']#keyword = '.\\'+dstReport['ScriptName'].split('\\')[-2] + '\\'+self.fill_txt(dstReport['ScriptName'])Hylink_Flag=Trueelifx==2:keyword='.\\'+dstReport['ScriptRestName'].split('\\')[-2]+'\\'+self.fill_txt(dstReport['ScriptRestName'])elifx==3:keyword=dstReport['ScriptRestName']Hylink_Flag=Trueelifx==4:keyword=self.get_log_file(dstReport['ScriptRestName'])Hylink_Flag=Trueelifx==5:keyword=dstReport['Result']else:keyword=''ifHylink_Flag==True:tmp_str_path='\\\\'.join(keyword.split('\\'))ifx==3:tmp_st2=self.fill_txt(dstReport['ScriptRestName'])try:self.sheet.write_merge(row,row,x,x,xlwt.Formula("HYPERLINK"+'("'+tmp_str_path+'";"'+tmp_st2+'")'),self.style6)exceptException,e:log_print(e)elifx==1:tmp_st2=self.fill_txt(dstReport['ScriptName'])try:self.sheet.write_merge(row,row,x,x,xlwt.Formula("HYPERLINK"+'("'+tmp_str_path+'";"'+tmp_st2+'")'),self.style11)exceptException,e:log_print(e)else:tmp_st2=self.fill_txt(dstReport['ScriptRestName'])try:self.sheet.write_merge(row,row,x,x,xlwt.Formula("HYPERLINK"+'("'+tmp_str_path+'";"'+tmp_st2+'")'),self.style7)exceptException,e:log_print(e)else:self.sheet.write(row,x,keyword,self.style1)self.dstRst_total_mod['DoneNum']=str(self.mod_row_offset)try:dstReport['Result'].find(KEY_RESULT_OK)exceptException,e:log_print(e)if(dstReport['Result'].find(KEY_RESULT_OK))>-1:self.dstRst_total_mod['OKNum']=str(string.atoi(self.dstRst_total_mod['OKNum'])+1)else:self.dstRst_total_mod['NGNum']=str(string.atoi(self.dstRst_total_mod['NGNum'])+1)self.write_model_done(row-self.mod_row_offset)