def test_spreadsheet_with_flag(self): """ Тест на использование класса SpreadsheetReport с переданным в конструктор wrapper-ом. """ src = self.test_files['test_xls.xls'] dst = os.path.join(self.dst_dir, 'res-test_xls.xls') if os.path.exists(dst): os.remove(dst) report = SpreadsheetReport(src, wrapper=DocumentXLS, type=FileConverter.XLS) section1 = report.get_section('Section1') section1.flush({'tag1': 1}) report.workbook.active_sheet = 1 section2 = report.get_section('Section2') for i in range(10): section2.flush({'tag2': i}) for i in range(10): section2.flush({'tag2': str(10)}, oriented=Section.HORIZONTAL) return report.build(dst)
def test_purchases_book(self): """ """ enterprise_name = u'Мегаэнтерпрайз' inn = 123123123 kpp = 123123 date_start = datetime.now() date_end = datetime.now() file_name = 'test-purchases_book.xlsx' template_name = self.test_files[file_name] report = SpreadsheetReport(template_name) header = report.get_section('header') header.flush({ 'enterprise_name': enterprise_name, 'inn': inn, 'kpp': kpp, 'date_start': date_start, 'date_end': date_end }) res_file_name = 'res-' + file_name dst = os.path.join(self.dst_dir, res_file_name) report.build(dst)
def test_merge_cells(self): src = self.test_files['test-merge-cells.xlsx'] dst = os.path.join(self.dst_dir, 'res-merge-cells.xlsx') report = SpreadsheetReport(src) report.get_section('head').flush({'kassa_za': u'Ноябрь'}) for i in range(10): report.get_section('table_dyn').flush({'doc_num': i}) report.get_section('foot').flush({'glavbuh': u'Иван'}) report.build(dst)
def test_rows_height(self): """ Тест на выставление высоты строк """ src = self.test_files['test_rows_height.xls'] dst = os.path.join(self.dst_dir, 'res-rows-height.xls') if os.path.exists(dst): os.remove(dst) report = SpreadsheetReport(src, wrapper=DocumentXLS, type=FileConverter.XLS) report.get_section('row1').flush({ 't1': LOREM_IPSUM[:20], 't2': u'. '.join([u'Проверка на автоподбор высоты', LOREM_IPSUM]) }) report.get_section('row2').flush({ 't1': LOREM_IPSUM[:20], 't2': u'. '.join( [u'Проверка на высоту строки, взятую из шаблона', LOREM_IPSUM]) }) report.build(dst)
def test_383_value(self): src = self.test_files['test-383.xlsx'] dst = os.path.join(self.dst_dir, 'res-383.xlsx') report = SpreadsheetReport(src) report.get_section('header').flush({'period': u'Ноябрь'}) for i in range(10): report.get_section('row').flush({'begin_year_debet': -i}) report.get_section('footer').flush({'glavbuh': u'Иван'}) report.build(dst)
def test_without_merge_cells(self): file_name = 'test-main_template.xlsx' template_name = self.test_files[file_name] report = SpreadsheetReport(template_name) head_section = report.get_section('head') for head in range(10): head_section.flush({'head_name': str(head)}, 1) #result_file, result_url = create_office_template_tempnames(template_name) res_file_name = 'res-' + file_name dst = os.path.join(self.dst_dir, res_file_name) report.build(dst) return res_file_name
def test_report_write(self): src = self.test_files['test-report-output.xlsx'] dst = os.path.join(self.dst_dir, 'res-report-output.xlsx') if os.path.exists(dst): os.remove(dst) self.assertEqual(os.path.exists(dst), False) report = SpreadsheetReport(src) self.test_left_down(report) self.test_right_up(report) self.test_vertical(report) self.test_horizontal(report) self.test_left_down2(report) return report.build(dst)
def test_report_write(self): src = self.test_files['test-report-output.xls'] dst = os.path.join(self.dst_dir, 'res-report-output.xls') if os.path.exists(dst): os.remove(dst) report = SpreadsheetReport(src, wrapper=DocumentXLS, type=FileConverter.XLS) self.test_left_down(report) self.test_right_up(report) self.test_vertical(report) self.test_horizontal(report) self.test_left_down2(report) return report.build(dst)
def test_cursor(self): """ Правильность вычисления курсора """ src = self.test_files['test-simple.xlsx'] dst = os.path.join(self.dst_dir, 'res-simple.xlsx') if os.path.exists(dst): os.remove(dst) self.assertEqual(os.path.exists(dst), False) report = SpreadsheetReport(src, tags=TemplateTags(test_tag=222)) section_a1 = report.get_section('A1') section_a1.flush({'user': u'Иванов Иван', 'date_now': 1}) # Проверяем курсор для колонки self.assertEqual(section_a1.sheet_data.cursor.row, ('A', 5)) # Проверяем курсор для строки self.assertEqual(section_a1.sheet_data.cursor.column, ('D', 1)) s_gor = report.get_section('GOR') s_gor.flush({'col': u'Данные'}, oriented=s_gor.HORIZONTAL) # Проверяем курсор для колонки self.assertEqual(s_gor.sheet_data.cursor.row, ('A', 5)) # Проверяем курсор для строки self.assertEqual(s_gor.sheet_data.cursor.column, ('E', 1)) for i in range(10): report.get_section('B1').flush({ 'nbr': i, 'fio': u'Иванов %d' % i, 'sectior': u'Какой-то сектор' }) self.assertEqual(s_gor.sheet_data.cursor.row, ('A', i + 6)) self.assertEqual(s_gor.sheet_data.cursor.column, ('D', i + 5)) s_gor_str = report.get_section('GorStr') s_gor_str.flush({'g': i + 1}, oriented=s_gor.HORIZONTAL) self.assertEqual(s_gor.sheet_data.cursor.row, ('A', i + 6)) self.assertEqual(s_gor.sheet_data.cursor.column, ('E', i + 5)) s_gor_str.flush({'g': i * i}, oriented=s_gor.HORIZONTAL) self.assertEqual(s_gor.sheet_data.cursor.row, ('A', i + 6)) self.assertEqual(s_gor.sheet_data.cursor.column, ('F', i + 5)) section_last = report.get_section('C1') section_last.flush({'user': u'Иван'}) self.assertEqual(section_last.sheet_data.cursor.row, ('A', 16)) self.assertEqual(section_last.sheet_data.cursor.column, ('D', 15))
def test_main_parameters(self): src = self.test_files['test-main_book.xlsx'] dst = os.path.join(self.dst_dir, 'res-main_book.xlsx') report = SpreadsheetReport(src) params_header = list(report.get_section('header').get_all_parameters()) self.assertEqual(0, len(params_header)) params_row = list(report.get_section('row').get_all_parameters()) self.assertEqual(13, len(params_row)) self.assertIn(u'#num#', params_row) self.assertIn(u'#account_name#', params_row) self.assertIn(u'#journal_num#', params_row) params_footer = list(report.get_section('footer').get_all_parameters()) self.assertEqual(12, len(params_footer)) self.assertIn(u'#begin_year_debet_sum#', params_footer) self.assertIn(u'#glavbuh#', params_footer) self.assertIn(u'#username#', params_footer)
def test_wide_cell_1(self): """ """ file_name = 'test-wide-section-1.xlsx' template_name = self.test_files[file_name] report = SpreadsheetReport(template_name) header = report.get_section(u'row') header.flush({}) self.assertEqual(header.sheet_data.cursor.row, ('A', 2)) self.assertEqual(header.sheet_data.cursor.column, ('B', 1)) header.flush({}, oriented=ISpreadsheetSection.HORIZONTAL) self.assertEqual(header.sheet_data.cursor.row, ('A', 2)) self.assertEqual(header.sheet_data.cursor.column, ('C', 1)) header.flush({}, oriented=ISpreadsheetSection.HORIZONTAL) self.assertEqual(header.sheet_data.cursor.row, ('A', 2)) self.assertEqual(header.sheet_data.cursor.column, ('D', 1)) header.flush({}) self.assertEqual(header.sheet_data.cursor.row, ('A', 3)) self.assertEqual(header.sheet_data.cursor.column, ('B', 2)) header.flush({}, oriented=ISpreadsheetSection.HORIZONTAL) self.assertEqual(header.sheet_data.cursor.row, ('A', 3)) self.assertEqual(header.sheet_data.cursor.column, ('C', 2)) header.flush({}) self.assertEqual(header.sheet_data.cursor.row, ('A', 4)) self.assertEqual(header.sheet_data.cursor.column, ('B', 3)) #result_file, result_url = create_office_template_tempnames(template_name) res_file_name = 'res-' + file_name dst = os.path.join(self.dst_dir, res_file_name) report.build(dst)
def test_xls_image_insertion(self): """ Вставка изображений """ src = self.test_files['test_insert_image.xls'] src_image1 = self.test_files['test_image1.bmp'] src_image2 = self.test_files['test_image2.bmp'] dst = os.path.join(self.dst_dir, 'res-insert_image.xls') if os.path.exists(dst): os.remove(dst) report = SpreadsheetReport(src, wrapper=DocumentXLS, type=FileConverter.XLS) row_section = report.get_section('row') row_section.flush({'image1': XLSImage(src_image1)}) row_section.flush({'image2': XLSImage(src_image2)}) row_section.flush({ 'image1': XLSImage(src_image2), 'image2': XLSImage(src_image1) }) report.build(dst)
def generate(self): """ Генерирует отчет и возвращает url для скачивания """ self._report = SpreadsheetReport(self.template_name) #main_rows, ppp_code = self._fetch_main_rows() self._create_header({}) self._create_main({}) self._create_middle({}) # Оборотки отключены и не дописаны, т.к. никто из аналитиков не может понять как их правильно собирать. facepalm! #self._create_first_oborot() #self._create_second_oborot() self._create_footer() return self._report
def create_pagebreaks_report(self, temp_path, res_path): section_params = {} report = SpreadsheetReport(temp_path) # проверим начальное количество разделителей rb = report.workbook.get_rowbreaks() self.assertEqual(len(rb), 0) cb = report.workbook.get_colbreaks() self.assertEqual(len(cb), 0) section = report.get_section(u'line') for i in range(20): section.flush(section_params) bottom_section = report.get_section(u'bottom') for i in range(2): bottom_section.flush(section_params) report.build(res_path) return report
def test_xls_formula_generation(self): """ Генерация формул в xls """ src = self.test_files['test-formula_generation.xls'] dst = os.path.join(self.dst_dir, 'res-formula_generation.xls') if os.path.exists(dst): os.remove(dst) report = SpreadsheetReport(src, wrapper=DocumentXLS, type=FileConverter.XLS) row_insert_formula_section = report.get_section('row_insert_formula') check_insert_formula_section = report.get_section( 'check_insert_formula') insert_formulas(row_insert_formula_section, check_insert_formula_section) report.build(dst) check_file = " Please, check output file %s with table proc." % dst if os.name == 'posix': openoffice = '/usr/bin/openoffice' libreoffice = '/usr/bin/libreoffice' office = None if os.path.exists(libreoffice): office = libreoffice elif os.path.exists(openoffice): office = openoffice assert office, ("Didn't found neither openoffice nor libreoffice" + check_file) subprocess.call([office, dst]) elif os.name == 'nt': subprocess.call(['EXCEL.exe', dst]) # elif os.name == 'mac': else: raise Exception("Can't check xls formula generation.")
def create_report(self, temp_path, res_path): """ """ header_params = {} #string_params = {} bottom_params = {} header_params[u'cashier'] = u'Иванов Иван Иваныч' #document.cashier #enterprise = Enterprise.objects.filter(id=document.enterprise_id) #if enterprise: header_params[ u'enterprise'] = u'Здесь название организации' #enterprise[0].name header_params[u'okpo'] = u'123947843' #enterprise[0].okpo #if document.number: header_params[u'number'] = '1' #document.number date = u'01-01-2001' #document.date_formatting base = u'Иванычу на похмел' #document.base if date: header_params[u'date'] = date bottom_params[u'date'] = date if base: header_params[u'base'] = base bottom_params[u'base'] = base report = SpreadsheetReport(temp_path) header = report.get_section(u'Header') header.flush(header_params) #Строки, соответствующие операциям #operations = AccEntry.objects.filter(document_id = document_id).select_related('debet_kbk', # 'credit_kbk', # 'debit_account', # 'credit_account') #total_summa = 1000.21 #for operation in operations: #if document.kvd: # kvd = ' %s ' %document.kvd.code #else: # kvd = u' ' kvd = u'1' debit_kbk_part = '123456789012' #operation.debet_kbk if operation.debet_kbk else '' debit_account = '20104' #operation.debet_account if operation.debet_account else '' credit_kbk_part = '976543223232' #operation.credit_kbk if operation.credit_kbk else '' credit_account = '40110' #operation.credit_account if operation.credit_account else '' debit_kbk = debit_kbk_part + kvd + debit_account credit_kbk = credit_kbk_part + kvd + credit_account #total_summa += operation.summa string_params = {u'debit_kbk': debit_kbk, u'kredit_kbk': credit_kbk} #if credit_account: accounting = credit_account #credit_account.code[-2:] string_params[u'accounting'] = accounting #if operation.summa: string_params[u'sum'] = 1000.21 #operation.summa string = report.get_section(u'String') for i in range(20): string.flush(string_params) #bottom_params = {u'total_sum': total_summa, # u'total_kopeks': two_decimal_kopeks(total_summa), # u'total_sum_in_words': money_in_words(total_summa) # } bottom_params = { u'total_sum': 1000.21, u'total_kopeks': 21, u'total_sum_in_words': u'Одна тысяча рублей', u'annex': 'annex', u'cashier': u'Иванов Иван Иваныч' } #if document.annex: #if document.cashier: bottom = report.get_section(u'Bottom') bottom.flush(bottom_params) report.build(res_path)
def test_work_document(self): if not self.test_connection(): return # with self.assertRaises(FileConverterException): # src = self.test_files['test-simple-converter.xls'] # report = SpreadsheetReport(src, converter=OpenOfficeConverter(port=8100)) src = self.test_files['test-simple-converter.xls'] report = SpreadsheetReport(src, converter=OpenOfficeConverter(port=2002)) dst = os.path.join(self.dst_dir, 'res-convert-simple.xlsx') if os.path.exists(dst): os.remove(dst) self.assertEqual(os.path.exists(dst), False) self.assertGreater(len(report._wrapper.sheets), 0) self.assertLessEqual(len(report._wrapper.sheets), 4) self.assertNotEqual(report._wrapper.workbook, None) self.assertNotEqual(report._wrapper.workbook.shared_strings, None) # Тестирование получения секции section_a1 = report.get_section('A1') self.assertIsInstance(section_a1, Section) with self.assertRaises(Exception): report.get_section('G1') section_a1.flush({'user': u'Иванов Иван', 'date_now': 1}) for i in range(10): report.get_section('B1').flush({'nbr': i, 'fio': u'Иванов %d' % i}) report.get_section('C1').flush({'user': u'Иван'}) report.build(dst, FileConverter.XLS) self.assertEqual(os.path.exists(dst), True)
def test_with_merge(self): """ Конструкция with merge для обьединения ячеек вывода """ src = self.test_files['test_merge.xls'] dst = os.path.join(self.dst_dir, 'res-merge.xls') if os.path.exists(dst): os.remove(dst) report = SpreadsheetReport(src, wrapper=DocumentXLS, type=FileConverter.XLS) s1 = report.get_section('s1') s2 = report.get_section('s2') s3 = report.get_section('s3') s4 = report.get_section('s4') s5 = report.get_section('s5') s5.flush({'p5': 1}, oriented=ISpreadsheetSection.VERTICAL) s5.flush({'p5': 2}, oriented=ISpreadsheetSection.VERTICAL) s5.flush({'p5': 3}, oriented=ISpreadsheetSection.HORIZONTAL) m1 = MergeXLS(s1, s2, {'p1': 1}, oriented=ISpreadsheetSection.HORIZONTAL) with m1: m2 = MergeXLS(s2, s3, { 'p21': 1, 'p22': 21 }, oriented=ISpreadsheetSection.HORIZONTAL) with m2: m3 = MergeXLS(s3, s4, {'p3': 1}, oriented=ISpreadsheetSection.HORIZONTAL) with m3: s4.flush({'p4': 1}, oriented=ISpreadsheetSection.RIGHT) for i in range(2, 4): s4.flush({'p4': i}, oriented=ISpreadsheetSection.VERTICAL) m3_exp = (m3._begin_merge_col == 9 and m3._end_merge_col == 9 and m3.begin_row_merge == 3 and m3.end_row_merge == 5) self.assertEqual(m3_exp, True) m3 = MergeXLS(s3, s4, {'p3': 2}, oriented=ISpreadsheetSection.HIERARCHICAL) with m3: s4.flush({'p4': 1}, oriented=ISpreadsheetSection.RIGHT) s4.flush({'p4': 2}, oriented=ISpreadsheetSection.VERTICAL) m2 = MergeXLS(s2, s3, { 'p21': 2, 'p22': 21 }, oriented=ISpreadsheetSection.HIERARCHICAL) with m2: m3 = MergeXLS(s3, s4, {'p3': 1}, oriented=ISpreadsheetSection.HORIZONTAL) with m3: s4.flush({'p4': 1}, oriented=ISpreadsheetSection.RIGHT) s4.flush({'p4': 2}, oriented=ISpreadsheetSection.VERTICAL) m1_exp = (m1._begin_merge_col == 6 and m1._end_merge_col == 6 and m1.begin_row_merge == 3 and m1.end_row_merge == 9) self.assertEqual(m1_exp, True) m1 = MergeXLS(s1, s2, {'p1': 2}, oriented=ISpreadsheetSection.HIERARCHICAL) with m1: m2 = MergeXLS(s2, s3, { 'p21': 1, 'p22': 21 }, oriented=ISpreadsheetSection.HORIZONTAL) with m2: m3 = MergeXLS(s3, s4, {'p3': 1}, oriented=ISpreadsheetSection.HORIZONTAL) with m3: s4.flush({'p4': 1}, oriented=ISpreadsheetSection.HORIZONTAL) return report.build(dst)
def test_workbook(self): src = self.test_files['test-simple.xlsx'] dst = os.path.join(self.dst_dir, 'res-simple.xlsx') if os.path.exists(dst): os.remove(dst) self.assertEqual(os.path.exists(dst), False) report = SpreadsheetReport(src, tags=TemplateTags(test_tag=222)) #self.assertGreater(len(report._wrapper.sheets), 0) self.assertNotEqual(report._wrapper.workbook, None) self.assertNotEqual(report._wrapper.workbook.shared_strings, None) # Тестирование получения секции section_a1 = report.get_section('A1') self.assertIsInstance(section_a1, Section) with self.assertRaises(SectionNotFoundException): report.get_section('G1') section_a1.flush({'user': u'Иванов Иван', 'date_now': 1}) s_gor = report.get_section('GOR') s_gor.flush({'col': u'Данные'}, oriented=s_gor.HORIZONTAL) for i in range(10): report.get_section('B1').flush({ 'nbr': i, 'fio': u'Иванов %d' % i, 'sector': u'Какой-то сектор' }) s_gor_str = report.get_section('GorStr') s_gor_str.flush({'g': i + i}, oriented=s_gor.HORIZONTAL) s_gor_str.flush({'g': i * i}, oriented=s_gor.HORIZONTAL) report.get_section('C1').flush({'user': u'Иван'}) with self.assertRaises(ReportGeneratorException): report.build(dst, FileConverter.XLS) report.build(dst) self.assertEqual(os.path.exists(dst), True)
def test_workbook_with_2_6_python(self): src = self.test_files['test-simple.xlsx'] dst = os.path.join(self.dst_dir, 'res-simple.xlsx') if os.path.exists(dst): os.remove(dst) self.assertEqual(os.path.exists(dst), False) report = SpreadsheetReport(src, tags=TemplateTags(test_tag=11)) section_a1 = report.get_section('A1') section_a1.flush({'user': u'Иванов Иван', 'date_now': 1}) s_gor = report.get_section('GOR') s_gor.flush({'col': u'Данные'}, oriented=s_gor.HORIZONTAL) for i in range(10): report.get_section('B1').flush({ 'nbr': i, 'fio': u'Иванов %d' % i, 'sector': u'Какой-то сектор' }) s_gor_str = report.get_section('GorStr') s_gor_str.flush({'g': i + i}, oriented=s_gor.HORIZONTAL) s_gor_str.flush({'g': i * i}, oriented=s_gor.HORIZONTAL) report.get_section('C1').flush({'user': u'Иван'}) report.build(dst) self.assertEqual(os.path.exists(dst), True)
def test_fake_section(self): src = self.test_files['test-simple-fake-section.xlsx'] with self.assertRaises(SectionException): report = SpreadsheetReport(src) report.build(src)
def test_formula_generation(self): src = self.test_files['test-formula_generation.xlsx'] dst = os.path.join(self.dst_dir, 'res-formula_generation.xlsx') if os.path.exists(dst): os.remove(dst) report = SpreadsheetReport(src) all_change = report.get_section('all_change') all_not_change = report.get_section('all_not_change') row_not_change = report.get_section('row_not_change') column_not_change = report.get_section('column_not_change') other_section = report.get_section('other_section') row_insert_formula_section = report.get_section('row_insert_formula') check_insert_formula_section = report.get_section( 'check_insert_formula') all_change_formula = '(A1+B1)*3' all_not_change_formula = '($A$1+B1)*3' row_not_change_formula = '(A$1+B1)*3' column_not_change_formula = '($A1+B1)*3' all_change.flush({ 'p1': 1, 'p2': 1 }, oriented=ISpreadsheetSection.VERTICAL) all_change.flush({ 'p1': 2, 'p2': 2 }, oriented=ISpreadsheetSection.VERTICAL) self.assertEqual( Formula.get_instance(all_change_formula).formula, '(A2+B2)*3') other_section.flush({'p1': 1}, oriented=ISpreadsheetSection.VERTICAL) all_change.flush({ 'p1': 3, 'p2': 3 }, oriented=ISpreadsheetSection.VERTICAL) self.assertEqual( Formula.get_instance(all_change_formula).formula, '(A4+B4)*3') other_section.flush({'p1': 1}, oriented=ISpreadsheetSection.VERTICAL) all_not_change.flush({ 'p1': 1, 'p2': 1 }, oriented=ISpreadsheetSection.VERTICAL) self.assertEqual( Formula.get_instance(all_not_change_formula).formula, '($A$1+B1)*3') all_not_change.flush({ 'p1': 2, 'p2': 2 }, oriented=ISpreadsheetSection.VERTICAL) self.assertEqual( Formula.get_instance(all_not_change_formula).formula, '($A$1+B2)*3') other_section.flush({'p1': 1}, oriented=ISpreadsheetSection.VERTICAL) all_not_change.flush({ 'p1': 3, 'p2': 3 }, oriented=ISpreadsheetSection.VERTICAL) self.assertEqual( Formula.get_instance(all_not_change_formula).formula, '($A$1+B4)*3') other_section.flush({'p1': 1}, oriented=ISpreadsheetSection.VERTICAL) row_not_change.flush({ 'p1': 1, 'p2': 1 }, oriented=ISpreadsheetSection.VERTICAL) self.assertEqual( Formula.get_instance(row_not_change_formula).formula, '(A$1+B1)*3') row_not_change.flush({ 'p1': 2, 'p2': 2 }, oriented=ISpreadsheetSection.VERTICAL) self.assertEqual( Formula.get_instance(row_not_change_formula).formula, '(A$1+B2)*3') other_section.flush({'p1': 1}, oriented=ISpreadsheetSection.VERTICAL) row_not_change.flush({ 'p1': 3, 'p2': 3 }, oriented=ISpreadsheetSection.VERTICAL) self.assertEqual( Formula.get_instance(row_not_change_formula).formula, '(A$1+B4)*3') other_section.flush({'p1': 1}, oriented=ISpreadsheetSection.VERTICAL) column_not_change.flush({ 'p1': 1, 'p2': 1 }, oriented=ISpreadsheetSection.VERTICAL) self.assertEqual( Formula.get_instance(column_not_change_formula).formula, '($A1+B1)*3') other_section.flush({'p1': 1}, oriented=ISpreadsheetSection.HORIZONTAL) column_not_change.flush({ 'p1': 2, 'p2': 2 }, oriented=ISpreadsheetSection.HORIZONTAL) self.assertEqual( Formula.get_instance(column_not_change_formula).formula, '($A1+F1)*3') other_section.flush({'p1': 1}, oriented=ISpreadsheetSection.HORIZONTAL) column_not_change.flush({ 'p1': 3, 'p2': 3 }, oriented=ISpreadsheetSection.HORIZONTAL) self.assertEqual( Formula.get_instance(column_not_change_formula).formula, '($A1+J1)*3') insert_formulas(row_insert_formula_section, check_insert_formula_section) # Проверяем, что вписанные формулы попали в дерево и правильно # записались found_B21 = found_C21 = False for row in report.sheets[0].sheet_data.write_data.getchildren(): if row.tag == 'row': for c_ in row.getchildren(): if c_.tag == 'c': if c_.attrib.get('r') == "B21": found_B21 = True func = c_.find('f') assert func is not None assert func.text == 'AVERAGE(B17:B20)' elif c_.attrib.get('r') == "C21": found_C21 = True func = c_.find('f') assert func is not None assert func.text == 'SUM((A17,A18,A19,A20))' assert found_B21 and found_C21 return report.build(dst)
def report_multi(cls, ids): import uuid import os from config import PATH_TO_GENERATE_INVOICE, PATH_WEB from excel.output import PATH_TEMPLATE from applications.good.service import GoodService COUNT_ROW = 42 COUNT_ROW2 = 91 file_name = str(uuid.uuid4()) + ".xlsx" path_to_target = os.path.join(PATH_TO_GENERATE_INVOICE, file_name) path = os.path.join(PATH_WEB, file_name) report = SpreadsheetReport(os.path.join( PATH_TEMPLATE, 'print_invoice2.xlsx')) for id in ids: waybill = WayBillService.get_by_id(id) sec_sub = report.get_section("sec1") sec_items = report.get_section("secitem") sec_data = {} sec_data['number'] = waybill.number sec_data['date'] = waybill.date.strftime('%d.%m.%Y').decode("utf-8") sec_data['point'] = waybill.pointsale_from.name sec_data['typeInv'] = waybill.type sec_data['rec'] = waybill.rec sec_sub.flush(sec_data) if waybill.type == RETAIL: for it in waybill.items: sec_items_data = { 'name': it.good.full_name, 'count': it.count or "", 'price_pay': GoodService.get_price(it.good_id).price_retail, 'mul': it.count * GoodService.get_price( it.good_id).price_retail if it.count else ''} sec_items.flush(sec_items_data) if waybill.items.count() <= COUNT_ROW: for i in xrange(COUNT_ROW - waybill.items.count()): sec_items.flush({'name': '', 'count': '', 'price_pay': '', 'mul': ''}) elif waybill.items.count() <= COUNT_ROW2: for i in xrange(COUNT_ROW2 - waybill.items.count()): sec_items.flush({'name': '', 'count': '', 'price_pay': '', 'mul': ''}) else: for it in waybill.items: sec_items_data = {'name': it.good.full_name, 'count': it.count or "", 'price_pay': GoodService.get_price(it.good_id).price_gross, 'mul': it.count * GoodService.get_price( it.good_id).price_gross if it.count else ''} sec_items.flush(sec_items_data) if waybill.items.count() <= COUNT_ROW: for i in xrange(COUNT_ROW - waybill.items.count()): sec_items.flush({'name': '', 'count': '', 'price_pay': '', 'mul': ''}) elif waybill.items.count() <= COUNT_ROW2: for i in xrange(COUNT_ROW2 - waybill.items.count()): sec_items.flush({'name': '', 'count': '', 'price_pay': '', 'mul': ''}) report.build(path_to_target) return path