def process_attachment_file(self, input_path, output_path, target=None): try: output_path, _ = os.path.splitext(output_path) output_path += '.DOCX' doc = DocxTemplate(input_path) rt = RichText() template_vars = mailer.render_DOC_template_var(self.application.config, target) rt.add('Alibaba',url_id=doc.build_url_id(template_vars['url']['webserver'])) context = {'target_name': template_vars['client']['first_name'], 'target_email_address': template_vars['client']['email_address'], 'secret_id': template_vars['uid'], 'example': rt } doc.render(context) doc.save(output_path) return output_path except: return 0
def make_doc_file(bkm_info_list): '''Gegernate a BKM meeting invitation message''' tpl = DocxTemplate('meeting_invitation_message_template.docx') result = [] for bkm_info in bkm_info_list: dic = {} dic['bkm_nums'] = bkm_info.get()[0] bkm_pdf_link = bkm_info.get()[5] rt = RichText() rt.add(bkm_info.get()[1], url_id=tpl.build_url_id(bkm_pdf_link), color='blue', underline=True) dic['bkm_titles'] = rt dic['authors'] = bkm_info.get()[2] dic['apprs'] = bkm_info.get()[3] dic['create_dates'] = bkm_info.get()[4] result.append(dic) print('成功获取BKM数据, 正在将数据写入模板......') context = {'bkm': result} tpl.render(context) tpl.save('meeting_invitation_message.docx')
rt = RichText() rt.add('a rich text', style='myrichtextstyle') rt.add(' with ') rt.add('some italic', italic=True) rt.add(' and ') rt.add('some violet', color='#ff00ff') rt.add(' and ') rt.add('some striked', strike=True) rt.add(' and ') rt.add('some small', size=14) rt.add(' or ') rt.add('big', size=60) rt.add(' text.') rt.add('\nYou can add an hyperlink, here to ') rt.add('google', url_id=tpl.build_url_id('http://google.com')) rt.add('\nEt voilà ! ') rt.add('\n1st line') rt.add('\n2nd line') rt.add('\n3rd line') rt.add('\n\n<cool>') rt.add('\nFonts :\n', underline=True) rt.add('Arial\n', font='Arial') rt.add('Courier New\n', font='Courier New') rt.add('Times New Roman\n', font='Times New Roman') rt.add('\n\nHere some') rt.add('superscript', superscript=True) rt.add(' and some') rt.add('subscript', subscript=True) rt_embedded = RichText('an example of ')
generated_path = get_full_path(r'generated.docx') context_path = get_full_path(r'inject.json') # Open document doc = DocxTemplate(template_path) # Read context replacements = read_json(context_path) # Replace variable context = copy.copy(replacements['variables']) # Replace images for image, data in replacements['images'].items(): context[image] = InlineImage(doc, data[0], width=Mm(data[1])) # Replace URLs for url, data in replacements['urls'].items(): rt = RichText() rt.add(data[0], url_id=doc.build_url_id(data[1])) context[url] = rt # Replace tables for table, data in replacements['tables'].items(): context[table + '_col_labels'] = data['col_labels'] context[table + '_tbl_contents'] = data['tbl_contents'] # Save new file doc.render(context) doc.save(generated_path)
def generate_detailed_report(project, outputname, sqc): doc = None print "> Calculating language metrics..." ncloc = sqc.get_loc(project[1]["key"]) languages = sqc.get_loc_lang_metrics(project[1]["key"], ncloc) ncloc = resources.span_number_format(int(ncloc)) print "> Retrieving issues..." vulnerabilities_num = sqc.get_issues_on_type( "VULNERABILITY,SECURITY_HOTSPOT", project[1]["key"]) vulnerabilities = resources.span_number_format(int(vulnerabilities_num)) bugs_num = sqc.get_issues_on_type("BUG", project[1]["key"]) bugs = resources.span_number_format(int(bugs_num)) code_smells_num = sqc.get_issues_on_type("CODE_SMELL", project[1]["key"]) code_smells = resources.span_number_format(int(code_smells_num)) print "> Retrieving components..." duplicated_blocks = resources.span_number_format( int(sqc.get_duplicated_blocks(project[1]["key"]))) duplicity = resources.span_number_format( float(sqc.get_duplicated_density(project[1]["key"]))) print "> Retrieving violated rules..." print " -> BUGS" bug_violations = sqc.get_violations(languages, "BUG", project[1]["key"]) print " -> VULNERABILITIES" vuln_violations = sqc.get_violations(languages, "VULNERABILITY,SECURITY_HOTSPOT", project[1]["key"]) print " -> CODE SMELL" cs_violations = sqc.get_violations(languages, "CODE_SMELL", project[1]["key"]) report_date_str = project[1]["lastAnalysisDate"] dt = maya.parse(report_date_str).datetime() report_date = str(dt.date().day) + "-" + resources.meses[str( dt.date().month)] + "-" + str(dt.date().year) + " " + str(dt.time()) hoy = date.today() today = str(hoy.day) + " de " + resources.meses[str(hoy.month) + "c"] + " de " + str( hoy.year) try: doc = DocxTemplate("templates/static_detail_report_template.docx") except: print "> Error. .docx template file not found." jinja_env = jinja2.Environment(extensions=['jinja2.ext.loopcontrols']) if doc: rt = RichText() rt.add( project[0], url_id=doc.build_url_id('http://srv-analiza/sonar/dashboard?id=' + project[1]["key"])) context = { 'today': today, 'project_name': project[0], 'rt': rt, 'report_date': report_date, 'ncloc': ncloc, 'lang_metrics': languages, 'vulnerabilities': vulnerabilities, 'bugs': bugs, 'code_smells': code_smells, 'duplicated_blocks': duplicated_blocks, 'duplicity': duplicity, 'bugs_violations': bug_violations, 'vuln_violations': vuln_violations, 'cs_violations': cs_violations, } doc.render(context, jinja_env) doc.save(outputname + ".docx") generate_charts(project, languages, sqc, vulnerabilities_num, bugs_num, code_smells_num, outputname)
OK = GREEN = '#008000' FAIL = RED = '#FF0000' TODO = YELLOW = '#FFFF00' DOING = CYAN = '#00FFFF' DONE = BLUE = '#0000FF' context['colors'] = [ {'status': 'OK', 'color': 'GREEN', 'bgcolor': OK}, {'status': 'FAIL', 'color': 'RED', 'bgcolor': FAIL}, {'status': 'TODO', 'color': 'YELLOW', 'bgcolor': TODO}, {'status': 'DOING', 'color': 'CYAN', 'bgcolor': DOING}, {'status': 'DONE', 'color': 'BLUE', 'bgcolor': DONE} ] # 渲染一个带有link的富文本 rt_link = RichText('You can add an hyperlink, here to ') rt_link.add('google', url_id=doc.build_url_id('http://google.com')) context['link'] = rt_link # 渲染一个非主流文字颜色的富文本 rt_color = RichText() rt_color.add(u'啊萨阿德\t', color=RED) rt_color.add('GREEN\t', color=GREEN) rt_color.add('YELLO\t', color=YELLOW) rt_color.add('CYAN\t', color=CYAN) rt_color.add('BLUE\t', color=BLUE) context['word_color'] = rt_color doc.render(context) doc.save("generated_doc.docx")