| 
						
						
							
								
							
						
						
					 | 
					 | 
					@ -8,11 +8,11 @@ from docx.enum.text import WD_PARAGRAPH_ALIGNMENT
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					def get_command_argv_by_sys():
 | 
					 | 
					 | 
					 | 
					def get_command_argv_by_sys():
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    # 默认值-用于测试
 | 
					 | 
					 | 
					 | 
					    # 默认值-用于测试
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    template_path = "template.docx"
 | 
					 | 
					 | 
					 | 
					    template_path = "template.docx"
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    template_path = "test.docx"
 | 
					 | 
					 | 
					 | 
					    #template_path = "test.docx"
 | 
				
			
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    filename_path = "demo.docx"
 | 
					 | 
					 | 
					 | 
					    filename_path = "demo.docx"
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    datafile_path = "data.json"
 | 
					 | 
					 | 
					 | 
					    datafile_path = "data.json"
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    datafile_path = "data_1.json"
 | 
					 | 
					 | 
					 | 
					    #datafile_path = "data_1.json"
 | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    cycle = 1
 | 
					 | 
					 | 
					 | 
					    cycle = 0
 | 
				
			
			
				
				
			
		
	
		
		
	
		
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    # 读取参数
 | 
					 | 
					 | 
					 | 
					    # 读取参数
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    number = len(sys.argv)
 | 
					 | 
					 | 
					 | 
					    number = len(sys.argv)
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    if 2 == number:
 | 
					 | 
					 | 
					 | 
					    if 2 == number:
 | 
				
			
			
		
	
	
		
		
			
				
					| 
						
							
								
							
						
						
							
								
							
						
						
					 | 
					 | 
					@ -61,18 +61,33 @@ def replace(template_path, filename_path, data_json):
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					
 | 
					 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    # 表格替换
 | 
					 | 
					 | 
					 | 
					    # 表格替换
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    for table in document_file.tables:
 | 
					 | 
					 | 
					 | 
					    for table in document_file.tables:
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					        for row in table.rows:
 | 
					 | 
					 | 
					 | 
					        # 字段
 | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					            for cell in row.cells:
 | 
					 | 
					 | 
					 | 
					        row = table.rows[0]
 | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					                for datum in data_json:
 | 
					 | 
					 | 
					 | 
					        group = None
 | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					                    if datum["key"] in cell.text:
 | 
					 | 
					 | 
					 | 
					        fields = []
 | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					                        if "text" == datum["type"]:
 | 
					 | 
					 | 
					 | 
					        for cell in row.cells:
 | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					                            cell.text = cell.text.replace(datum["key"], datum["value"])
 | 
					 | 
					 | 
					 | 
					            split = cell.text.replace("}","").split(".")
 | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					                        elif "picture" == datum["type"]:
 | 
					 | 
					 | 
					 | 
					            group = split[0] + "}"
 | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					                            paragraph = cell.paragraphs[0]
 | 
					 | 
					 | 
					 | 
					            field = split[1]
 | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					                            paragraph.clear()
 | 
					 | 
					 | 
					 | 
					            fields.append(field.strip())
 | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					                            paragraph.add_run().add_picture(datum["value"], width=Inches(datum["width"]))
 | 
					 | 
					 | 
					 | 
					
 | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					                        elif "number" == datum["type"]:
 | 
					 | 
					 | 
					 | 
					        # 替换
 | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					                            cell.text = cell.text.replace(datum["key"], str(datum["value"]))
 | 
					 | 
					 | 
					 | 
					        for datum in data_json:
 | 
				
			
			
				
				
			
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					            if datum["key"] in group:
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					                rows = len(table.rows)
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					                values = datum["value"]
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					                for z in range(0, len(values)):
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					                    if z < rows:
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					                       row = table.rows[z]
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					                    else:
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					                        row = table.add_row()
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					                    item = values[z]
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					                    for i in range(0, len(fields)):
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					                        field = fields[i]
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					                        if item.get(field) is not None:
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					                            row.cells[i].text = str(item.get(field))
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    document_file.save(filename_path)
 | 
					 | 
					 | 
					 | 
					    document_file.save(filename_path)
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    return 0
 | 
					 | 
					 | 
					 | 
					    return 0
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					
 | 
					 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
	
		
		
			
				
					| 
						
							
								
							
						
						
							
								
							
						
						
					 | 
					 | 
					@ -149,12 +164,6 @@ def foreach(template_path, filename_path, data_json):
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    return 0
 | 
					 | 
					 | 
					 | 
					    return 0
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					
 | 
					 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					
 | 
					 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					
 | 
					 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					
 | 
					 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					
 | 
					 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					
 | 
					 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					
 | 
					 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					
 | 
					 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					def main():
 | 
					 | 
					 | 
					 | 
					def main():
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    params = get_command_argv_by_sys()
 | 
					 | 
					 | 
					 | 
					    params = get_command_argv_by_sys()
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    [template, filename, datafile, cycle] = params
 | 
					 | 
					 | 
					 | 
					    [template, filename, datafile, cycle] = params
 | 
				
			
			
		
	
	
		
		
			
				
					| 
						
							
								
							
						
						
						
					 | 
					 | 
					
 
 |