Package: custom-py-docx
CreateDocxinitdef__init__(self,doc_path='',doc_name='new_doc',font_name='Tahoma',font_size=11)->None:self.doc=Document()self.doc_name=doc_nameself.doc_path=doc_pathself.font_name=font_nameself.font_size=font_sizeself.set_font()set_fontdefset_font(self):font=self.doc.styles['Normal'].fontfont.name=self.font_namefont.size=Pt(self.font_size)save_docdefsave_doc(self):self.doc.save(f'{self.doc_path}{self.doc_name}.docx')delete_docdefdelete_doc(doc_path):os.remove(doc_path)concat_docsdefconcat_docs(self,doc1_path,doc2_path,new_doc_path,new_doc_name):doc1=Document(doc1_path)doc2=Document(doc2_path)composer=Composer(doc1)composer.append(doc2)composer.save(f'{new_doc_path}/{new_doc_name}')break_pagedefbreak_page(self):self.doc.add_page_break()add_headingdefadd_heading(self,text,level):self.doc.add_heading(text,level)add_imagedefadd_image(self,image_path_name,widht_inches=3.0,height_inches=3.0):self.doc.add_picture(image_path_name,width=Inches(widht_inches),height=Inches(height_inches))last_paragraph=self.doc.paragraphs[-1]last_paragraph.alignment=WD_ALIGN_PARAGRAPH.CENTERadd_paragraphdefadd_paragraph(self,text:str,bold=False,italic=False,set_color={'r':0,'g':0,'b':0}):paragraph=self.doc.add_paragraph()run=paragraph.add_run(text)run.font.bold=boldrun.font.italic=italicrun.font.color.rgb=RGBColor(set_color['r'],set_color['g'],set_color['b'])returnparagraphconcat_on_paragraphdefconcat_on_paragraph(self,paragraph,text,bold=False,italic=False,set_color={'r':0,'g':0,'b':0}):run=paragraph.add_run(text)run.font.bold=boldrun.font.italic=italicrun.font.color.rgb=RGBColor(set_color['r'],set_color['g'],set_color['b'])create_tabledefcreate_table(self,table_title,table_title_level,style='Medium Shading 1',column_names=[],rows=()):self.doc.add_heading(table_title,table_title_level)table=self.doc.add_table(rows=1,cols=len(column_names),style=style)row=table.rows[0].cellsforiinrange(len(column_names)):row[i].text=column_names[i]row_len=len(rows[0])forrow_datainrows:row=table.add_row().cellsrow_index=0whilerow_index<row_len:row[row_index].text=str(row_data[row_index])row_index+=1add_pizza_plotdefadd_pizza_plot(self,x_values,y_values,width=450,height=400,plot_name=''):fig=px.pie(names=x_values,values=y_values,width=width,height=height)fig.update_traces(textposition='inside',textinfo='percent+label')img_bytes=fig.to_image(format='png',engine='kaleido')withopen(f'{plot_name}.png','wb')asfile:file.write(img_bytes)self.doc.add_picture(f'{plot_name}.png')add_bar_plotdefadd_bar_plot(self,x_values:List[Union[str,int,float]],y_values:List[Union[int,float]],colors:Optional[List[str]]=None,y_title:Optional[str]=None,x_title:Optional[str]=None,plot_name:Optional[str]='plot',random_colors:Optional[bool]=False,base_color:Optional[str]=None)->None:fig=go.Figure()ifcolorsisnotNone:fig.add_trace(go.Bar(x=x_values,y=y_values,marker=dict(color=colors)))elifrandom_colors:num_bars=len(x_values)ifbase_colorisnotNone:base_hsl=colorsys.rgb_to_hls(*tuple((int(base_color[i:i+2],16)foriin(0,2,4))))colors=[f'rgb{tuple(map(int,colorsys.hls_to_rgb(base_hsl[0],max(0,min(1,base_hsl[1]+random.uniform(-0.2,0.2))),max(0,min(1,base_hsl[2]+random.uniform(-0.2,0.2))))))}'for_inrange(num_bars)]else:colors=[f'rgb({random.randint(0,255)},{random.randint(0,255)},{random.randint(0,255)})'for_inrange(num_bars)]fig.add_trace(go.Bar(x=x_values,y=y_values,marker=dict(color=colors)))else:fig.add_trace(go.Bar(x=x_values,y=y_values))fig.update_layout(xaxis_title=x_title,yaxis_title=y_title,width=500,height=500,margin=dict(l=30,r=30,t=30,b=30),font=dict(size=12))img_bytes=fig.to_image(format='png',engine='kaleido')withopen(f'{plot_name}.png','wb')asfile:file.write(img_bytes)self.doc.add_picture(f'{plot_name}.png')Exemplos de usoif__name__=="__main__":doc=CreateDocx('./','testing_new_doc_class')doc.add_heading('heading',1)paragraph=doc.add_paragraph('Criticidade: ',bold=True,italic=True)doc.concat_on_paragraph(paragraph,'Critical',bold=True,set_color={'r':192,'g':0,'b':0})doc.add_image('images/142776859-7c6c3ef6-6a15-4e34-99f6-b4bac029a036.png',8.0,6.0)doc.break_page()doc.create_table(table_title='Testing Table',table_title_level=1,column_names=['column1','column2','column3','column4','quantity'],rows=(('dado 1','dado 2','dado 3','dado 4',100),('dado 1','dado 2','dado 3','dado 4',10),('dado 1','dado 2','dado 3','dado 4',410),('dado 1','dado 2','dado 3','dado 4',50),('dado 1','dado 2','dado 3','dado 4',10),('dado 1','dado 2','dado 3','dado 4',100),('dado 1','dado 2','dado 3','dado 4',100),('dado 1','dado 2','dado 3','dado 4',100),('dado 1','dado 2','dado 3','dado 4',100),('dado 1','dado 2','dado 3','dado 4',100),('dado 1','dado 2','dado 3','dado 4',100),('dado 1','dado 2','dado 3','dado 4',100),('dado 1','dado 2','dado 3','dado 4',100),('dado 1','dado 2','dado 3','dado 4',100),('dado 1','dado 2','dado 3','dado 4',100),('dado 1','dado 2','dado 3','dado 4',100),('dado 1','dado 2','dado 3','dado 4',100),('dado 1','dado 2','dado 3','dado 4',100),('dado 1','dado 2','dado 3','dado 4',100),))doc.add_heading('OWASP Top 10 Pizza Plot',1)doc.add_pizza_plot(x_values=['A01:2021','A02:2021','A03:2021','A04:2021','A05:2021','A06:2021','A07:2021','A08:2021','A09:2021','A10:2021'],y_values=[100,40,60,10,30,10,2,5,102,11],plot_name='owasp_top_10_pizza')doc.add_heading('OWASP Top 10 Bar Plot',1)doc.add_bar_plot(x_values=['A01:2021','A02:2021','A03:2021','A04:2021','A05:2021','A06:2021','A07:2021','A08:2021','A09:2021','A10:2021'],y_values=[100,40,60,10,30,10,2,5,102,11],plot_name='owasp_top_10_bar',random_colors=True)doc.save_doc()