我们从Python开源项目中,提取了以下9个代码示例,用于说明如何使用xlwt.Formula()。
def create_simple_xls(self, **kw): font0 = xlwt.Font() font0.name = 'Times New Roman' font0.colour_index = 2 font0.bold = True style0 = xlwt.XFStyle() style0.font = font0 style1 = 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")) return wb, 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. """ for skip_reason in (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_SKIP for result_type in self.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 += 1 if result_type == self.SKIPPED: self._create_skipped_reason_summary()
def test_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)
def do_fill_workbook(self, workbook): """export to excel all articles tin stock""" sheet = workbook.add_sheet(_(u"Stock")) line = 0 columns = [ _(u'Id'), _(u'Name'), _(u'Category'), _(u'Purchase price'), _(u"Stock count"), _(u'Stock threshold'), _(u'Value'), ] for col, label in enumerate(columns): self.write_cell(sheet, 0, col, label, style=self.get_header_style()) for line, item in enumerate(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) if item.category else '') sheet.write(line + 1, 3, item.purchase_price if item.purchase_price else 0) sheet.write(line + 1, 4, '' if item.stock_count is None else item.stock_count) sheet.write(line + 1, 5, '' if item.stock_count is None else item.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)))
def external_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() if request.method == 'POST': selected_for_payment = [] for checklist in checklists: if request.POST.get('pay_%s' % checklist.pk, False): selected_for_payment.append(checklist) reviewers = User.objects.filter(pk__in=[c.user.pk for c in selected_for_payment]).distinct() xls = SimpleXLS() xls.write_row(0, (_('amt.'), _('reviewer'), _('EC-Nr.'), _('sum'), _('IBAN'), _('SWIFT-BIC'), _('Social Security Number')), header=True) for i, reviewer in enumerate(reviewers): checklists = reviewer.checklist_set.filter(pk__in=[c.pk for c in selected_for_payment]) xls.write_row(i + 1, [ len(checklists), str(reviewer), ", ".join(c.submission.get_ec_number_display() for c in checklists), len(checklists) * price.price, reviewer.profile.iban, reviewer.profile.swift_bic, reviewer.profile.social_security_number, ]) r = len(reviewers) + 1 xls.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') for checklist in selected_for_payment: ChecklistBillingState.objects.update_or_create( checklist=checklist, defaults={'billed_at': doc.date}) payment = ChecklistPayment.objects.create(document=doc) payment.checklists = selected_for_payment return redirect('ecs.billing.views.view_checklist_payment', payment_pk=payment.pk) return render(request, 'billing/external_review.html', { 'checklists': checklists, 'price': price, })
def write_model_value(self,mod_name,loop,dstReport): #print 'Fuction write_model_value' row = self.start_module_row + self.mod_row_offset keyword = None for x in range(0,9): Hylink_Flag = False if x ==0: keyword = self.dstRst_total_mod['DoneNum'] elif x ==1: keyword = dstReport['ScriptName'] #keyword = '.\\'+dstReport['ScriptName'].split('\\')[-2] + '\\'+self.fill_txt(dstReport['ScriptName']) Hylink_Flag = True elif x ==2: keyword = '.\\'+dstReport['ScriptRestName'].split('\\')[-2] + '\\'+self.fill_txt(dstReport['ScriptRestName']) elif x ==3: keyword = dstReport['ScriptRestName'] Hylink_Flag = True elif x ==4: keyword = self.get_log_file(dstReport['ScriptRestName']) Hylink_Flag = True elif x ==5: keyword = dstReport['Result'] else: keyword ='' if Hylink_Flag==True: tmp_str_path ='\\\\'.join(keyword.split('\\')) if x==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) except Exception ,e: log_print(e) elif x ==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) except Exception,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) except Exception ,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) except Exception,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)