import docx import os import datetime def rewrite(modelpath, id:str,envs, rsts,clus, images,type=0): # doc = docx.Document("../../model.docx") print("load doc") if(type == 0): doc = docx.Document(modelpath+"/model.docx") else: doc = docx.Document(modelpath+"/model1.docx") print("open doc") # doc.paragraphs[1].text = doc.paragraphs[1].text.replace("tanceqibianhao",id) today = datetime.date.today() doc.tables[0].rows[0].cells[1].text = id doc.tables[0].rows[0].cells[3].text = str(today.year)+"."+str(today.month)+"."+str(today.day) # doc.paragraphs[1].text += str(today.year)+"."+str(today.month)+"."+str(today.day) print("input date and id ") for i in range(13): doc.tables[1].rows[i].cells[1].text = envs[i] print(len(rsts)) for i in range(12+2): doc.tables[2].rows[i].cells[1].text = rsts[i] print(i) doc.tables[3].rows[0].cells[0].text += clus[0] doc.tables[3].rows[2].cells[0].text += clus[1] doc.tables[3].rows[4].cells[0].text += clus[2] doc.tables[3].rows[6].cells[0].text += clus[3] doc.tables[3].rows[0].cells[1].text = str(int(clus[4])) doc.tables[3].rows[1].cells[1].text = str(int(clus[5])) doc.tables[3].rows[1].cells[3].text = str(int(clus[6])) doc.tables[3].rows[1].cells[5].text = str(int(clus[7])) doc.tables[3].rows[2].cells[1].text = str(int(clus[8])) doc.tables[3].rows[3].cells[1].text = str(int(clus[9])) doc.tables[3].rows[3].cells[3].text = str(int(clus[10])) doc.tables[3].rows[3].cells[5].text = str(int(clus[11])) doc.tables[3].rows[4].cells[1].text = str(int(clus[12])) doc.tables[3].rows[5].cells[1].text = str(int(clus[13])) doc.tables[3].rows[5].cells[3].text = str(int(clus[14])) doc.tables[3].rows[5].cells[5].text = str(int(clus[15])) doc.tables[3].rows[6].cells[1].text = str(int(clus[16])) doc.tables[3].rows[7].cells[1].text = str(int(clus[17])) doc.tables[3].rows[7].cells[3].text = str(int(clus[18])) doc.tables[5].rows[0].cells[0].paragraphs[0].runs[0].clear() doc.tables[5].rows[0].cells[0].paragraphs[0].runs[0].add_picture(images[0],width=docx.shared.Cm(5),height=docx.shared.Cm(5)) doc.tables[5].rows[0].cells[1].paragraphs[0].runs[0].clear() doc.tables[5].rows[0].cells[1].paragraphs[0].runs[0].add_picture(images[1],width=docx.shared.Cm(5),height=docx.shared.Cm(5)) doc.tables[5].rows[0].cells[2].paragraphs[0].runs[0].clear() doc.tables[5].rows[0].cells[2].paragraphs[0].runs[0].add_picture(images[2],width=docx.shared.Cm(5),height=docx.shared.Cm(5)) doc.tables[5].rows[1].cells[0].paragraphs[0].runs[0].clear() doc.tables[5].rows[1].cells[0].paragraphs[0].runs[0].add_picture(images[3],width=docx.shared.Cm(5),height=docx.shared.Cm(5)) doc.tables[5].rows[1].cells[1].paragraphs[0].runs[0].clear() doc.tables[5].rows[1].cells[1].paragraphs[0].runs[0].add_picture(images[4],width=docx.shared.Cm(5),height=docx.shared.Cm(5)) doc.tables[5].rows[1].cells[2].paragraphs[0].runs[0].clear() doc.tables[5].rows[1].cells[2].paragraphs[0].runs[0].add_picture(images[5],width=docx.shared.Cm(5),height=docx.shared.Cm(5)) doc.tables[7].rows[0].cells[0].paragraphs[0].runs[0].clear() doc.tables[7].rows[0].cells[0].paragraphs[0].runs[0].add_picture(images[6],width=docx.shared.Cm(5),height=docx.shared.Cm(5)) doc.tables[7].rows[0].cells[1].paragraphs[0].runs[0].clear() doc.tables[7].rows[0].cells[1].paragraphs[0].runs[0].add_picture(images[7],width=docx.shared.Cm(5),height=docx.shared.Cm(5)) doc.tables[7].rows[0].cells[2].paragraphs[0].runs[0].clear() doc.tables[7].rows[0].cells[2].paragraphs[0].runs[0].add_picture(images[8],width=docx.shared.Cm(5),height=docx.shared.Cm(5)) doc.tables[7].rows[1].cells[0].paragraphs[0].runs[0].clear() doc.tables[7].rows[1].cells[0].paragraphs[0].runs[0].add_picture(images[9],width=docx.shared.Cm(5),height=docx.shared.Cm(5)) doc.tables[7].rows[1].cells[1].paragraphs[0].runs[0].clear() doc.tables[7].rows[1].cells[1].paragraphs[0].runs[0].add_picture(images[10],width=docx.shared.Cm(5),height=docx.shared.Cm(5)) doc.tables[7].rows[1].cells[2].paragraphs[0].runs[0].clear() doc.tables[7].rows[1].cells[2].paragraphs[0].runs[0].add_picture(images[11],width=docx.shared.Cm(5),height=docx.shared.Cm(5)) doc.paragraphs[12].runs[0].clear() doc.paragraphs[12].runs[0].add_picture(images[12],width=docx.shared.Cm(15.24),height=docx.shared.Cm(15.24)) if type != 0: # doc.tables[4].rows[0].cells[2].text = "噪声等效温差(NETD)" # temppara = doc.tables[4].rows[0].cells[2].paragraphs[0]._element.clone() # temppara.text = "噪声等效温差(NETD)" # doc.tables[4].rows[0].cells[2]._body.insert(1,temppara) doc.tables[2].add_row() doc.tables[2].add_row() # doc.tables[2].rows[12+2].cells[0].text = doc.tables[2].rows[10+2].cells[0].text # doc.tables[2].rows[12+2].cells[1].text = doc.tables[2].rows[10+2].cells[1].text # doc.tables[2].rows[13+2].cells[0].text = doc.tables[2].rows[11+2].cells[0].text # doc.tables[2].rows[13+2].cells[1].text = doc.tables[2].rows[11+2].cells[1].text doc.tables[2].rows[10+4].cells[0].text = "平均峰值探测率" doc.tables[2].rows[11+4].cells[0].text = "由平均噪声、平均摆幅计算NETD" doc.tables[2].rows[10+4].cells[1].text = rsts[10+4] doc.tables[2].rows[11+4].cells[1].text = rsts[11+4] # doc.paragraphs[0].text = doc.paragraphs[0].text.replace("短","中") #标题位置为 0 pass # 修改这里 for i in range(4): for row in doc.tables[i].rows: # 修改表格高度 # doc.tables[1].rows[i].height = docx.shared.Cm(0.3) # pass for cell in row.cells: paragraph = cell.paragraphs[0] paragraph.paragraph_format.space_before = docx.shared.Pt(0) paragraph.paragraph_format.space_after = docx.shared.Pt(0) filename = id+"测试报告.docx" doc.save(filename) return