def add_table_merge(path, data, merges, widths=[]): ''' 创建表格 ''' logger.info('--生成合并表格{}'.format(datetime.now())) labels = data['labels'] rows = data['data'] docx = Document() style = docx.styles['Table Grid'] table = docx.add_table(rows=1 + len(rows), cols=len(labels), style=style) # 自定义表格宽度 if widths: table.autofit = False # 很重要! for row in range(1 + len(rows)): for index, w in enumerate(widths): table.cell(row, index).width = Inches(float(w)) # 生成表头 hdr_cells = table.rows[0].cells for index, value in enumerate(labels): if not isinstance(value, unicode): value = unicode(value, "utf-8") hdr_cells[index].text = value format_data = formate_table_merge_data(data=rows, merges=merges) logger.info('--格式化表格数据{}'.format(datetime.now())) # 生成表格内容 create_docx_table_body(docx=docx, path=path, table=table, data=format_data, merges=merges) logger.info('--合并表格完成{}'.format(datetime.now())) docx.save(path) return path
def createSaveDoc(updatedEmail): def checkDuplicateTitle(title): for root, dirs, files in os.walk(os.getcwd()): match = 0 for name in files: matchobj = re.match(title, name) if matchobj: print(match) return match document = Document() # creates doc object document.add_paragraph(updatedEmail) # adds string to doc titleOfDoc = "MPPG credentials for " + getNameOfSite(updatedEmail) + ' ' + getCity(updatedEmail) match = checkDuplicateTitle(titleOfDoc) if match == 0: document.save(titleOfDoc + '.docx') else: titleOfDoc = (titleOfDoc + ' (' + str(match) + ')' + '.docx') document.save(titleOfDoc)