def process_attachment_file(self, input_path, output_path, target=None): output_path, _ = os.path.splitext(output_path) output_path += '.pdf' pdf_file = platypus.SimpleDocTemplate(output_path, pagesize=letter, rightMargin=72, leftMargin=72, topMargin=72, bottomMargin=18) url = self.application.config['mailer.webserver_url'] if target is not None: url += '?uid=' + target.uid try: pdf_template = self.get_template(input_path, url) pdf_file.multiBuild(pdf_template) except Exception as err: self.logger.error('failed to build the pdf document', exc_info=True) return self.logger.info('wrote pdf file to: ' + output_path + ('' if target is None else ' with uid: ' + target.uid)) return output_path
def saveas(self, filename, header=None, footer=None): #print "BODY:\n",self.body.toxml(),"\n" self.rldoc = platypus.SimpleDocTemplate(filename) # overwrites the default UL : #self.stylesheet.UL = BulletListStyle(bulletWidth=12) self.header = header self.footer = footer for k, v in self.getElementStyle(self.body).items(): setattr(self.rldoc, k, v) story = [] for e in self.body.content: style = self.getElementStyle(e) if style.pageBreakBefore: if len(story) \ and story[-1].__class__ != platypus.PageBreak: story.append(platypus.PageBreak()) #print e.toxml() story += self.elem2flow(e, style, self.getDocumentWidth()) if style.pageBreakAfter: if len(story) \ and story[-1].__class__ != platypus.PageBreak: story.append(platypus.PageBreak()) #print story #for fl in story: # assert hasattr(fl,"wrapOn") self.rldoc.build(story, onFirstPage=self.onEveryPage, onLaterPages=self.onEveryPage)
def create_pdf(self, invoice, header_image): self.is_valid(invoice, raise_exception=True) # Create the document template buff = io.BytesIO() doc = platypus.SimpleDocTemplate(buff, title=invoice.description, pagesize=A4) invoice_settings = InvoiceSettings.objects.settings() vat_settings = VatSettings.objects.settings() # Container for the 'Flowable' objects elements = [] elements.append( self._table_header(invoice, invoice_settings, vat_settings, header_image)) elements.append(platypus.Spacer(1, 12)) elements.append(self._table_lines(invoice)) elements.append(self._table_totals(invoice)) for text in self._text_footer(invoice_settings.footer): elements.append(self._para(text)) # write the document to disk doc.build(elements, canvasmaker=NumberedCanvas) pdf = buff.getvalue() buff.close() invoice_filename = '{}.pdf'.format(invoice.invoice_number) invoice.pdf.save(invoice_filename, ContentFile(pdf)) return invoice_filename
def pdf_export(self, with_child=False): doc = pl.SimpleDocTemplate(self.filename) elements = self.pdf_elements() if with_child and self.child: elements += self.child.pdf_elements() ## write document to disk doc.build(elements, onFirstPage=myFirstPage(self.title), onLaterPages=myLaterPages) print(self.filename, " exportiert")
def save(self, directory, filetype): """Saves the attributes of the Country to a specified file. Parameters ---------- directory : str Path to save output filetype : str For output file with country data (json or pdf) Raises ------ NotImplementedError If unsupported filetype is passed in. """ filename = directory + self.name.replace(' ', '_') if filetype == 'json': filename += ".json" with open(filename, 'w') as f: json.dump(vars(self), f, indent=4) f.close() elif filetype == 'pdf': ss = getSampleStyleSheet() pdf = platy.SimpleDocTemplate(filename + ".pdf") flowables = [] flowables.append(platy.Paragraph(self.name, ss['Heading1'])) for k in vars(self): if k == 'id' or k == 'name': continue if type(vars(self)[k]) is str or type(vars(self)[k]) is int: p = f"{k.replace('_',' ').title()}: {str(vars(self)[k])}" flowables.append(platy.Paragraph(p, ss['BodyText'])) else: p = f"{k.replace('_',' ').title()}:" flowables.append(platy.Paragraph(p, ss['BodyText'])) bullets = [] for v in vars(self)[k]: p = v if type(vars(self)[k]) is not list: p += ": "+vars(self)[k][v] b = platy.Paragraph(p, ss['Normal']) bullets.append(platy.ListItem(b, leftIndent=35)) table = platy.ListFlowable(bullets, bulletType='bullet') flowables.append(table) pdf.build(flowables) elif filetype == 'html': filename += ".json" json_string = json.dumps(vars(self),indent=4) f = open(filename, 'w') f.write("var data = " + json_string) f.close() else: raise NotImplementedError( f"Output file type, {filetype}, not supported")
def toPdf(): doc = platypus.SimpleDocTemplate('test.pdf', topMargin=0.9 * inch, bottomMargin=0.9 * inch, title='DaDa Math', author='qyb') elements = [] for i in range(1): elements.append(genTable()) elements.append(platypus.flowables.PageBreak()) doc.build(elements)
def create_book(self): logger.debug('Binding %s.pdf', self.file_name) pdf_path = os.path.join(self.app.output_path, 'pdf') file_path = os.path.join(pdf_path, self.file_name + '.pdf') logger.debug('Writing %s', file_path) os.makedirs(pdf_path, exist_ok=True) # , pagesize = reportlab.lib.pagesizes.portrait(reportlab.lib.pagesizes.A4)) self.book = pdf.SimpleDocTemplate(file_path) self.book.lang = 'en' self.book.title = self.book_title self.book.author = 'Lightnovel Crawler'
def saveas(self,filename): self.rldoc = platypus.SimpleDocTemplate(filename) # overwrites the default UL self.stylesheet.UL = BulletListStyle(bulletWidth=12) for k,v in self.body.stylesheet.Document.items(): setattr(self.rldoc,k,v) self.rldoc.build(self.body, onFirstPage=self.onEveryPage, onLaterPages=self.onEveryPage)
def render_document(filename: str, document: DG.Document) -> platypus.BaseDocTemplate: """Create a platypus document from a Document""" pagesize = (document.pagesize.width().points(), document.pagesize.height().points()) return platypus.SimpleDocTemplate( filename, pagesize=pagesize, topMargin=(document.top_margin.points()), bottomMargin=(document.bottom_margin.points()), leftMargin=(document.left_margin.points()), rightMargin=(document.right_margin.points()), title=document.title)
def generate_report(fn=None): if fn is None: timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M") fn = f"report_{timestamp}.pdf" doc = platypus.SimpleDocTemplate( fn, pagesize=pagesizes.letter, ) builder = _build_report() doc.build(builder, onFirstPage=page_footer, onLaterPages=page_footer) print(f"Wrote report to {fn}") return fn
def pdf_response_rl(self, question, filename, download=False): """ Use the ReportLab api and the Model Objects provided by the context dict in this views pipeline. """ import reportlab.platypus as RL import reportlab.lib.styles as SL from io import BytesIO flo = [] out = BytesIO() # create a byte buffer to hold PDF data pdf = RL.SimpleDocTemplate(out) pdf.showBoundary = 1 pdf.leftMargin = pdf.rightMargin = pdf.leftMargin / 2 pdf.topMargin = pdf.bottomMargin = pdf.topMargin / 2 ss = SL.getSampleStyleSheet() h1 = RL.Paragraph("Question:", ss['Heading1']) h2 = RL.Paragraph(f"{str(question)}", ss['Heading2']) h3 = RL.Paragraph("Choices:", ss['Heading3']) hl = RL.HRFlowable(width='100%') if question.question_img: img = RL.Image(question.question_img.path, width=100, height=75, hAlign="LEFT") flo += [img, hl] flo += [h1, h2, hl, h3] ps = [ RL.Paragraph(f"{str(c)} [{c.votes} votes]", ss['BodyText']) for c in question.choice_set.all() ] ul = [RL.ListItem(x, value='sparkle') for x in ps] lf = RL.ListFlowable(ul, bulletType='bullet') flo += [lf] flo += [hl] try: pdf.build(flo) except Exception as e: message = (f"Unable to generate PDF from <hl><pre>{html}</pre>") return HttpResponse(message, status=500) out.seek(0) # reset byte buffer pointer to the start if download: return FileResponse(out, as_attachment=True, filename=filename) return FileResponse(out, filename=filename)
def simple_gen_testpage(fname): """Generate a simple test page""" # NOTE: this example taken from # https://www.blog.pythonlibrary.org/2010/09/21/reportlab-tables-creating-tables-in-pdfs-with-python/ doc = plat.SimpleDocTemplate(fname, pagesize=letter) elements = [] data = [['00', '01', '02', '03', '04'], ['10', '11', '12', '13', '14'], ['20', '21', '22', '23', '24'], ['30', '31', '32', '33', '34']] t = plat.Table(data) t.setStyle( plat.TableStyle([('BACKGROUND', (1, 1), (-2, -2), colors.green), ('TEXTCOLOR', (0, 0), (1, -1), colors.red)])) elements.append(t) # write the document to disk doc.build(elements)
def save(self, add_preamble=True): """Saves PDF Parameters ---------- add_preamble : bool Either to add preamble containing title/date/author information """ if self.title is None: title = self.name + " report" else: title = self.title pageinfo = self.name + " data" ##REF: Name was automagically refactored def my_first_page(canvas, doc): canvas.saveState() canvas.setFont(self.font, 16) canvas.drawCentredString(self.pagesize[0] / 2.0, self.pagesize[1] - 108, title) canvas.setFont(self.font, 9) canvas.drawString(rplu.inch, 0.75 * rplu.inch, "First Page / %s" % pageinfo) canvas.restoreState() ##REF: Name was automagically refactored def my_later_pages(canvas, doc): canvas.saveState() canvas.setFont(self.font, 9) canvas.drawString(rplu.inch, 0.75 * rplu.inch, "Page %d %s" % (doc.page, pageinfo)) canvas.restoreState() filename = self._filename + ".pdf" doc = rplp.SimpleDocTemplate(filename) story = self._story if add_preamble: story = self.__preamble + story if __debug__ and not self in debug.handlers: debug("REP", "Saving the report into %s" % filename) doc.build(story, onFirstPage=my_first_page, onLaterPages=my_later_pages)
def QuickReport(conn, fileName, *args, **kwargs): from reportlab.lib import colors from reportlab.lib.styles import getSampleStyleSheet from reportlab.lib.units import inch styles = getSampleStyleSheet() title = 'Db Report' if 'title' in kwargs: title = kwargs['title'] del kwargs['title'] names = [x.upper() for x in conn.GetColumnNames()] try: smiCol = names.index('SMILES') except ValueError: try: smiCol = names.index('SMI') except ValueError: smiCol = -1 if smiCol > -1: if hasCDX: tform = CDXImageTransformer(smiCol) elif 1: tform = ReportLabImageTransformer(smiCol) else: tform = CactvsImageTransformer(smiCol) else: tform = None kwargs['transform'] = tform tbl = conn.GetReportlabTable(*args, **kwargs) tbl.setStyle( platypus.TableStyle([ ('GRID', (0, 0), (-1, -1), 1, colors.black), ('FONT', (0, 0), (-1, -1), 'Times-Roman', 8), ])) if smiCol > -1 and tform: tbl._argW[smiCol] = tform.width * 1.2 elements = [tbl] reportTemplate = PDFReport() reportTemplate.pageHeader = title doc = platypus.SimpleDocTemplate(fileName) doc.build(elements, onFirstPage=reportTemplate.onPage, onLaterPages=reportTemplate.onPage)
def pdf_maker(trialnumber, ptname, footer): ''' Makes a PDF with a top text, image, and bottom text. Inputs:- trialnumber: number of trials to plot (indexing from zero) ptname: patient name (string) to put in top text footer: text string for bottom text ''' from reportlab.lib.styles import ParagraphStyle, getSampleStyleSheet import reportlab.platypus as platypus from reportlab.lib.units import cm fileprefix = 'emg-plot-' filesuffix = '.png' toptext = '<font size=14><b>EMG for %s</b></font>' % ptname pdf_path = tkFileDialog.asksaveasfilename(initialdir='C:', initialfile='EMG report %s.pdf' %ptname) doc = platypus.SimpleDocTemplate(pdf_path, topMargin = cm, bottomMargin = cm, rightMargin = cm, leftMargin = cm) story = [] # Make new, indented text style. style = getSampleStyleSheet()['Normal'] indented = ParagraphStyle( 'indented', parent=style, leftIndent=2*cm ) for n in range(trialnumber+1): story.append(platypus.Paragraph(toptext, indented)) story.append(platypus.Spacer(1,0.1*cm)) graph = platypus.Image(str(fileprefix+str(n)+filesuffix)) graph._restrictSize(19*cm, 24*cm) story.append(graph) #story.append(platypus.Spacer(1,0.5*cm)) footer = footer.replace('\n','<br />\n') #Reportlab's Paragraph ignores newlines story.append(platypus.Paragraph(footer, indented)) story.append(platypus.PageBreak()) print('docbuild ran in %s' %pdf_path) doc.build(story)
def generic_feature_report(feature_object, output_file): import reportlab.platypus as platypus from reportlab.lib.styles import getSampleStyleSheet elements = [] doc = platypus.SimpleDocTemplate(output_file) style_sheet = getSampleStyleSheet() # pge_img = platypus.Image('https://www.cecsb.org/wp-content/uploads/2013/05/PGE-LOGO-1024x259.png') # elements.append(pge_img) elements.append(platypus.Paragraph('FEATURE REPORT',style_sheet['Heading1'])) table = platypus.Table(feature_object.build_field_order()) elements.append(table) if feature_object.att_res: for att in feature_object.att_res: elements.append(platypus.Image(att['DOWNLOAD_URL'])) doc.build(elements) return(output_file)
def report(self, invoice, user, response): self._is_valid(invoice, raise_exception=True) # Create the document template doc = platypus.SimpleDocTemplate( response, title='Report - Invoice Time Analysis', pagesize=A4 ) # Container for the 'Flowable' objects elements = [] elements.append(self._head( 'Time analysis by user and ticket for invoice {}'.format( invoice.invoice_number ) )) elements.append(platypus.Spacer(1, 12)) elements.append(self._table_lines(invoice)) elements.append(self._para( 'Printed {} by {}'.format(timezone.now(), user.username) )) doc.build(elements)
def build_pdf(self, target=None): output_pdf = self.config['output_pdf'] pdf_file = platypus.SimpleDocTemplate(output_pdf, pagesize=letter, rightMargin=72, leftMargin=72, topMargin=72, bottomMargin=18) url = self.application.config['mailer.webserver_url'] if target is not None: url += '?uid=' + target.uid pdf_template = self.get_template(url) try: pdf_file.multiBuild(pdf_template) except Exception as err: self.logger.error('failed to build the pdf document', exc_info=True) return False self.logger.info('wrote pdf file to: ' + output_pdf + ('' if target is None else ' with uid: ' + target.uid)) return True
def __createPdfFromImages(self, src_images_path): doc = platypus.SimpleDocTemplate(self.__pdfPath) doc.leftMargin = 0 doc.bottomMargin = 0 doc.rightMargin = 0 doc.topMargin = 0 pageWidth = PAGE_WIDTH_MM * self.__scale story = [] hasStory = False i = 1 for image_file in src_images_path: try: pilImg = Image.open(image_file) print('Adding ' + basename(image_file) + ' to pdf document...') except Exception: print('Cannot access a file: ' + image_file) continue imageWidth = pilImg.size[0] print('imageWidth:', imageWidth) imageHeight = pilImg.size[1] print('imageHeight:', imageHeight) # desc = 'Paragraph number desc_file = join(dirname(image_file), DESC_FILE_NAME) print('desc_file:', desc_file) desc = None if exists(desc_file): desc = read_desc(desc_file) print('desc:', desc) if desc is not None and not DESC_DONE.get(desc_file, False): para = Paragraph(desc, style_heading1) story.append(para) DESC_DONE[desc_file] = True #put different spaces before first and second images on every page if not i % 2: story.append( platypus.Spacer(1, self.__space_before_image1 * inch)) else: story.append( platypus.Spacer(1, self.__space_before_image2 * inch)) repImg = platypus.Image(image_file, pageWidth, pageWidth * (imageHeight / imageWidth)) story.append(repImg) #put some space after every image if not i % 2: story.append( platypus.Spacer(1, self.__space_after_image1 * inch)) else: story.append( platypus.Spacer(1, self.__space_after_image2 * inch)) #break page after every 2 images if not i % 2: story.append(platypus.PageBreak()) i += 1 print('OK') hasStory = True doc.build(story) if hasStory: print("Pdf file was created successfully") else: print("Pdf file was not created") if exists(self.__pdfPath): remove(self.__pdfPath) return hasStory
def write_report_two_columns(cfg_dct, res_lst, mut_lst, fname, sample_name=None): """Generate a PDF report to a given output file name """ col_tab = cfg_dct["resistance_level_colours"] level_coltab = dict([(k, (colors.HexColor(v[1]), colors.HexColor(v[2]))) for k, v in col_tab.items()]) doc = plat.SimpleDocTemplate( fname, pagesize=letter, title="basespace HIV drug resistance genotype report", author="BCCfE in HIV/AIDS") # get the actual text width, (not the page width): txt_w = page_w - doc.leftMargin - doc.rightMargin w_half, top_table_col_width = txt_w * 0.5, txt_w / 3.3333 doc_els = [plat.Spacer(1, 1.5 * cm)] ti_style = ParagraphStyle("scotitle", alignment=TA_CENTER, fontSize=20) doc_els.append(plat.Paragraph(cfg_dct["report_title"], ti_style)) re_style = ParagraphStyle("scored", fontSize=15, textColor=colors.red, spaceBefore=5 * mm, spaceAfter=5 * mm) doc_els.append(plat.Paragraph("For research use only", re_style)) # -- top table doc_els.append(top_table(sample_name, top_table_col_width)) lc, rc = 0, 1 big_table, btstyle = [], [] # now drug classes tables, two per line known_dc_lst = cfg_dct["known_dclass_list"] # from the resistance, we determine which drug_classes to write a table for: # we only write a table if we are given resistance data for it. got_dc_set = set([dc["drug_class"] for dc in res_lst]) tl = [ drug_class_table(cfg_dct, dc, level_coltab, w_half) for dc in known_dc_lst if dc in got_dc_set ] d_rowmin = 0 while len(tl) > 0: row_lst = [tl.pop(0)] if len(tl) > 0: row_lst.append(tl.pop(0)) else: row_lst.append("") big_table.append(row_lst) d_rowmax = len(big_table) - 1 btstyle.extend([("ALIGN", (lc, d_rowmin), (lc, d_rowmax), "RIGHT"), ("ALIGN", (rc, d_rowmin), (rc, d_rowmax), "LEFT"), ('VALIGN', (lc, d_rowmin), (rc, d_rowmax), 'TOP')]) # this is for layout debugging # big_table = [["l0", "r0"], ["l1", "r1"], ["l2", "r2"]] # debug_lst = [("GRID", (lc, 0), (rc, d_rowmax), 1, colors.red)] # btstyle.extend(debug_lst) assert sum( len(row) == 2 for row in big_table) == len(big_table), "big_table lines are wonky" doc_els.append( plat.Table(big_table, style=btstyle, colWidths=[w_half, w_half], hAlign="CENTRE")) # bottom paragraphs doc_els.append(bottom_para(cfg_dct["disclaimer_text"])) doc_els.append(bottom_para(cfg_dct["generated_by_text"])) doc.build(doc_els)
def makeDoc(self, docElements, title, outFilename): doc = platypus.SimpleDocTemplate(outFilename, title=title) doc.build(docElements)
def save(e, apt_store, vuln_store, filename, output_type): """ Saves the event information to a specified file. Parameters ---------- e : stix2 Sighting object The event to save apt_store : FileSystemStore/Source object STIX formatted threat actors vuln_store : FileSystemSource object STIX formatted vulnerabilities filename : string The path and name of the output file output_type : str For output file with country data (json or pdf) """ r_num = str(e.first_seen).replace('-', '').replace(' ', '_').replace(':', '') if 'vulnerability' in e.sighting_of_ref: p = "References" vuln = vuln_store.query(Filter("id", "=", e.sighting_of_ref))[0] p2 = vuln.name + ": " + vuln.description else: p = "Possible attribution" p2 = apt_store.query(Filter("id", "=", e.sighting_of_ref))[0].name event_dict = { "report number": r_num[:15], "date": str(e.first_seen)[:19], "description": e.description, p: p2 } if output_type == "pdf": ss = getSampleStyleSheet() flowables = [] flowables.append( platy.Paragraph(f"Report #{r_num[:15]}", ss['Heading1'])) flowables.append(platy.Paragraph("Date:", ss['Italic'])) flowables.append( platy.Paragraph(str(e.first_seen)[:19], ss['BodyText'])) flowables.append(platy.Paragraph("Description:", ss['Italic'])) flowables.append(platy.Paragraph(e.description, ss['BodyText'])) flowables.append(platy.Paragraph(p, ss['Italic'])) flowables.append(platy.Paragraph(p2, ss['BodyText'])) pdf = platy.SimpleDocTemplate(filename + r_num[:15] + '.pdf') pdf.build(flowables) elif output_type == "json": with open(filename + r_num[:15] + ".json", 'w') as f: json.dump(event_dict, f) f.close() elif output_type == 'html': f = open(filename + r_num[:15] + ".json", 'w') f.write("var data = " + str(event_dict)) f.close() else: raise NotImplementedError( f"Output file type, {filetype}, not supported")
#生成一个有 10 列的表格数据 data.append(items[i:i + 5] + items[i + 10:i + 15]) # 跳过没答案的数据 table = platypus.Table(data, 0.5 * inch, 0.19 * inch, [('FONT', (0, 0), (-1, -1), 'Courier')]) # 每个cell 0.5' 宽,0.19' 高,差不多 100 题排满一张 A4 # Courier 是等宽字体,为了俺的算式看起来整齐 # (0,0)/(-1,-1)说的是font style运用范围,从左上到右下 return table fname = 'asmd_with_answer.pdf' title = 'Math' author = 'qyt' doc = platypus.SimpleDocTemplate(fname, topMargin=1 * inch, bottomMargin=0.8 * inch, title=title, author=author) # 目标是一个叫 fname 的 PDF 文件,缺省上下留白有点多,修改为上1',下0.8',由于 100 道题撑不满整整一页,所以不对称的页眉页脚高度显示出来上下的留白几乎一样 elements = [] n = 10 for i in range(n): items = genList(100 * 2) #随机生成 100 道题目(其实是 200 道题,无答案 100 道,带答案 100 道) elements.append(genTable(items)) elements.append(platypus.flowables.PageBreak()) elements.append(genTable_with_answer(items)) elements.append(platypus.flowables.PageBreak()) # 生成 2n 页的数据,每个表格后面跟着一个换页 doc.build(elements)
def write_report_one_column(cfg_dct, res_lst, mut_lst, fname, sample_name=None): """Generate a PDF report to a given output file name """ col_tab = cfg_dct["resistance_level_colours"] level_coltab = dict([(k, (colors.HexColor(v[1]), colors.HexColor(v[2]))) for k, v in col_tab.items()]) doc = plat.SimpleDocTemplate(fname, pagesize=letter, topMargin=1 * cm, title="basespace drug resistance report", author="BCCfE") # get the actual text width, (not the page width): txt_w = page_w - doc.leftMargin - doc.rightMargin table_width = txt_w - 1 * cm doc_els = [] ti_style = ParagraphStyle("scotitle", alignment=TA_CENTER, fontSize=20) doc_els.append(plat.Paragraph(cfg_dct["report_title"], ti_style)) re_style = ParagraphStyle("scored", fontSize=15, textColor=colors.red, spaceBefore=5 * mm, spaceAfter=5 * mm) doc_els.append(plat.Paragraph("For research use only", re_style)) # -- top table doc_els.append(top_table(sample_name, table_width)) # now drug classes tables, two per line known_dc_lst = cfg_dct["known_dclass_list"] # from the resistance, we determine which drug_classes to write a table for: # we only write a table if we are given resistance data for it. got_dc_set = set([dc["drug_class"] for dc in res_lst]) tot_tab, tot_style = [], [] for dc in [dc for dc in known_dc_lst if dc in got_dc_set]: tl, t_style = drug_class_tablst(len(tot_tab), cfg_dct, dc, level_coltab, compact=True) tot_tab.extend(tl) tot_style.extend(t_style) # adjust the overall table style num_rows = len(tot_tab) tot_style.extend([("VALIGN", (0, 0), (1, num_rows - 1), "TOP"), ("FONTSIZE", (0, 0), (1, num_rows - 1), TAB_FONT_SIZE), ("LEADING", (0, 0), (1, num_rows - 1), TAB_FONT_SIZE)]) left_col_w = table_width * 0.36 right_col_w = table_width - left_col_w doc_els.append( plat.Table(tot_tab, vAlign="TOP", hAlign="CENTRE", style=tot_style, colWidths=[left_col_w, right_col_w])) # this is for layout debugging # big_table = [["l0", "r0"], ["l1", "r1"], ["l2", "r2"]] # debug_lst = [("GRID", (lc, 0), (rc, d_rowmax), 1, colors.red)] # btstyle.extend(debug_lst) # bottom paragraphs doc_els.append(bottom_para(cfg_dct["disclaimer_text"])) doc_els.append(bottom_para(cfg_dct["generated_by_text"])) doc.build(doc_els)
def save(actor, directory, filetype, fs_gen, fs_real): """Saves the attributes of the Threat Actor to a specified file. Parameters ---------- actor : stix2 ThreatActor object the threat actor to save directory : str Path to save output filetype : str For output file with country data (json or pdf) fs_gen : FileSystemStore object Data store with info about threat actor fs_real : FileSystemSource object Data store with reference information about real world data """ filename = directory + actor.name.replace(' ', '') relationships = fs_gen.query([ Filter("type", "=", "relationship"), Filter("source_ref", "=", actor.id)]) ttps, tools, malwares = [], [], [] for r in relationships: if "attack-pattern" in r.target_ref: ttps.append(r.target_ref) elif "tool" in r.target_ref: tools.append(r.target_ref) elif "malware" in r.target_ref: malwares.append(r.target_ref) elif "location" in r.target_ref: countries = fs_gen.query([ Filter("type", "=", "location"), Filter("id", "=", r.target_ref)]) tool_names = [fs_real.query([ Filter("type", "=", "tool"), Filter("id", "=", t)])[0].name for t in tools] m_s = [fs_real.query([ Filter("type", "=", "malware"), Filter("id", "=", m)])[0].name for m in malwares] actor_dict = { "name": actor.name, "aliases": actor.aliases, "first_seen": actor.first_seen.strftime("%B %Y"), "last_seen": actor.last_seen.strftime("%B %Y"), "attribution": countries[0].name, "resource_level": actor.resource_level, "primary motivation": actor.primary_motivation, "secondary motivations": actor.secondary_motivations, "description": actor.description, "ttps": ttps, "tools": tool_names, "malware": m_s } if filetype == 'json': with open(filename+".json", 'w') as f: json.dump(actor_dict, f) f.close() elif filetype == 'pdf': ss = getSampleStyleSheet() pdf = platy.SimpleDocTemplate(filename + ".pdf") flowables = [] try: actor_dict = json.loads(actor.description) p = ( f'{actor.name} is a {actor_dict["actor_type"]} group also ' f'known as {" or ".join(actor.aliases)}. It was first seen in ' f'{actor.first_seen.strftime("%B %Y")}, and is attributed to ' f'the state of {countries[0].name}. Its level of ' f'sophistication is {actor_dict["sophistication"]}, and its ' f'primary motivation is {actor.primary_motivation}, though it ' f'is sometimes motivated by ' f'{" and ".join(actor.secondary_motivations)}.' ) except json.decoder.JSONDecodeError: actor_dict = None p = actor.description flowables.append(platy.Paragraph(actor.name, ss['Heading1'])) flowables.append(platy.Paragraph(p, ss['BodyText'])) if actor_dict is not None: p = actor.name + "'s goals are " flowables.append(platy.Paragraph(p, ss['BodyText'])) bullets = [] for g in actor.goals: p = platy.Paragraph(g, ss['Normal']) bullets.append(platy.ListItem(p, leftIndent=35)) flowables.append(platy.ListFlowable(bullets, bulletType='bullet')) flowables.append(platy.Paragraph('Tools', ss['Heading2'])) p = "This threat actor is known to use the following tools." flowables.append(platy.Paragraph(p, ss['BodyText'])) bullets = [] for tool in sorted(tool_names[:10], key=str.casefold): p = platy.Paragraph(tool, ss['Normal']) bullets.append(platy.ListItem(p, leftIndent=35)) flowables.append(platy.ListFlowable(bullets, bulletType='bullet')) flowables.append(platy.Paragraph('Malware', ss['Heading2'])) p = "This threat actor is known to use the following malware." flowables.append(platy.Paragraph(p, ss['BodyText'])) bullets = [] for malware in sorted(m_s[:5], key=str.casefold): p = platy.Paragraph(malware, ss['Normal']) bullets.append(platy.ListItem(p, leftIndent=35)) flowables.append(platy.ListFlowable(bullets, bulletType='bullet')) flowables.append( platy.Paragraph('Attack Patterns', ss['Heading2'])) for t in ttps: ttp = fs_real.query([ Filter("type", "=", "attack-pattern"), Filter("id", "=", t)])[0] p = f"{ttp.name}" flowables.append(platy.Paragraph(p, ss['Italic'])) p = [] for ref in ttp.external_references: if ref.source_name == "mitre-attack": p.append("Mitre Attack: "+ref.external_id) elif ref.source_name == "capec": p.append(ref.external_id) if len(p) > 0: p = "(" + ", ".join(p) + ")\n" flowables.append(platy.Paragraph(p, ss['BodyText'])) else: try: flowables.append(platy.Paragraph( ttp.description+'\n', ss['BodyText'])) except AttributeError: pass flowables.append(platy.Paragraph("", ss['Heading2'])) flowables.append( platy.Paragraph('Related Reporting', ss['Heading2'])) p = f"These reported incidents are likely or highly likely to be \ attributed to {actor.name}, though there may be others:" flowables.append(platy.Paragraph(p, ss['BodyText'])) sightings = fs_gen.query([ Filter("type", "=", "sighting"), Filter("sighting_of_ref", "=", actor.id)]) r_nums = [ str(s.first_seen).replace('-', '') for s in sightings][:15] bullets = [] for r in sorted(r_nums): p = platy.Paragraph( r.replace(' ', '_').replace(':', '')[:15], ss['Normal']) bullets.append(platy.ListItem(p, leftIndent=35)) flowables.append(platy.ListFlowable(bullets, bulletType='bullet')) pdf.build(flowables) elif filetype == 'html': filename += ".json" f = open(filename, 'w') f.write("var data = " + str(actor_dict)) f.close() else: raise NotImplementedError( f"Output file type, {filetype}, not supported")
py.pytesseract.tesseract_cmd = "C:\\Program Files\\Tesseract-OCR\\tesseract.exe" #print("The current working directory is", os.getcwd()); files = [] #Configure Path path = "D:\Study\Blockchain\Images" # r=root, d=directories, f = files for r, d, f in os.walk(path): for file in f: img = cv2.imread(os.path.join(r, file)) if img is not None: files.append(os.path.join(r, file)) doc1 = rs.SimpleDocTemplate("BlockchainImages.pdf", pagesize=pages.A4, rightMargin=72, leftMargin=72, topMargin=72, bottomMargin=18) Story = [] ##Initialization for Table Paragraph = rs.Paragraph styles = getSampleStyleSheet() style_right = ParagraphStyle(name='right', parent=styles['Normal'], alignment=TA_RIGHT) for i in range(0, len(files) - 1): im = rs.Image(os.path.join(files[i]), 7 * inch, 4 * inch) text = str(((py.image_to_string(Image.open(os.path.join(files[i]))))))
def save_org(org, directory, filetype, assessment): """ Saves information about the organization to a file. Paramters --------- org : stix2 Identity object the organization to save directory : str Path to save output filetype : str Type of output file (json or pdf) assessment : dictionary representation of NIST 800-171 assessment table """ filename = directory + org.name.replace(' ', '') org_desc = json.loads(org.description) org_dict = { "name": org.name, "background": org_desc['Background'], "network_size": org_desc['Network']['size'], "vulnerability_level": org_desc['Security Posture']['vulnerability'], "NIST vulnerabilities": org_desc['Security Posture']['vulns'], "sectors": org.sectors } if filetype == 'json': with open(filename+".json", 'w') as f: json.dump(org_dict, f) f.close() elif filetype == 'pdf': ss = getSampleStyleSheet() pdf = platy.SimpleDocTemplate(filename + ".pdf") flowables = [] flowables.append(platy.Paragraph(org.name, ss['Heading1'])) p = f'Sector: {", ".join(org.sectors)}' flowables.append(platy.Paragraph(p, ss['BodyText'])) flowables.append(platy.Paragraph("Background", ss['Heading2'])) p = ( f'{org.name} is headquartered in the country of ' f'{org_desc["Background"]["headquarters"]}. It has ' f'{org_desc["Background"]["number of employees"]} employees and an' f' annual revenue of {org_desc["Background"]["annual revenue"]}.' ) flowables.append(platy.Paragraph(p, ss['BodyText'])) flowables.append(platy.Paragraph("Computer Network", ss['Heading2'])) p = f"Network size: {org_desc['Network']['size']} (on a scale of 100)" flowables.append(platy.Paragraph(p, ss['BodyText'])) p = "NIST 800-171 Security Evaluation Results" flowables.append(platy.Paragraph(p, ss['Heading2'])) p = f"Score: {org_desc['Security Posture']['vulnerability']}/100" flowables.append(platy.Paragraph(p, ss['BodyText'])) p = "Vulnerabilities (failed requirements):" flowables.append(platy.Paragraph(p, ss['BodyText'])) bullets = [] nist_reqs = {} for cat in assessment: for req in assessment[cat]: nist_reqs[req["Requirement"]] = req["Description"] for vuln in org_desc['Security Posture']['vulns']: p = platy.Paragraph(f'({vuln}) {nist_reqs[vuln]}', ss['Normal']) bullets.append(platy.ListItem(p, leftIndent=35)) flowables.append(platy.ListFlowable(bullets, bulletType='bullet')) pdf.build(flowables) elif filetype == 'html': filename += ".json" f = open(filename, 'w') f.write("var data = " + str(org_dict)) f.close() else: raise NotImplementedError( f"Output file type, {filetype}, not supported")
def write_report_one_column(report_templates, fname, sample_name=None): """Generate a PDF report to a given output file name """ doc = plat.SimpleDocTemplate(fname, pagesize=letter, topMargin=1 * cm, title="basespace drug resistance report", author="BCCfE") # get the actual text width, (not the page width): # noinspection PyUnresolvedReferences txt_w = page_w - doc.leftMargin - doc.rightMargin table_width = txt_w - 1 * cm doc_els = [] ti_style = ParagraphStyle("scotitle", alignment=TA_CENTER, fontSize=20) re_style = ParagraphStyle("scored", fontSize=15, textColor=colors.red, spaceBefore=5 * mm, spaceAfter=5 * mm) failure_template = None for report_template in report_templates: if 'failure_message' in report_template.virus_config: failure_template = report_template continue # from the resistance, we determine which drug_classes to write a table for: # we only write a table if we are given resistance data for it. reported_genotypes = report_template.get_reported_genotypes() if not reported_genotypes: continue for genotype in reported_genotypes: if doc_els: doc_els.append(PageBreak()) got_dc_set = report_template.get_reported_drug_classes(genotype) cfg_dct = report_template.virus_config col_tab = cfg_dct["resistance_level_colours"] level_coltab = dict([(k, (colors.HexColor(v[1]), colors.HexColor(v[2]))) for k, v in col_tab.items()]) append_header(doc_els, cfg_dct, table_width, re_style, ti_style, sample_name, genotype) # now drug classes tables, two per line known_dc_lst = cfg_dct["known_dclass_list"] tot_tab, tot_style = [], [] for dc in [dc for dc in known_dc_lst if dc in got_dc_set]: tl, t_style = drug_class_tablst(len(tot_tab), report_template, genotype, dc, level_coltab) tot_tab.extend(tl) tot_style.extend(t_style) # adjust the overall table style num_rows = len(tot_tab) tot_style.extend([ ("VALIGN", (0, 0), (1, num_rows - 1), "TOP"), ("FONTSIZE", (0, 0), (1, num_rows - 1), TAB_FONT_SIZE), ("LEADING", (0, 0), (1, num_rows - 1), TAB_FONT_SIZE) ]) left_col_w = table_width * 0.5 right_col_w = table_width - left_col_w doc_els.append( plat.Table(tot_tab, vAlign="TOP", hAlign="CENTRE", style=tot_style, colWidths=[left_col_w, right_col_w])) # this is for layout debugging # big_table = [["l0", "r0"], ["l1", "r1"], ["l2", "r2"]] # debug_lst = [("GRID", (lc, 0), (rc, d_rowmax), 1, colors.red)] # btstyle.extend(debug_lst) append_footer(doc_els, cfg_dct) assert failure_template is not None if not doc_els: cfg_dct = failure_template.virus_config append_header(doc_els, cfg_dct, table_width, re_style, ti_style, sample_name) doc_els.append( plat.Paragraph("Sequence does not meet quality-control standards", re_style)) append_footer(doc_els, cfg_dct) doc.build(doc_els)