parent
							
								
									89ce3f1e7a
								
							
						
					
					
						commit
						746240af00
					
				@ -1,2 +1,3 @@
 | 
				
			|||||||
/build/
 | 
					/build/
 | 
				
			||||||
/dist/
 | 
					/dist/
 | 
				
			||||||
 | 
					.idea
 | 
				
			||||||
@ -1,33 +0,0 @@
 | 
				
			|||||||
from docx import Document
 | 
					 | 
				
			||||||
from docx.shared import Inches
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
def replace(template ,filename, data):
 | 
					 | 
				
			||||||
    doc = Document(template)
 | 
					 | 
				
			||||||
    # 段落替换
 | 
					 | 
				
			||||||
    for paragraph in doc.paragraphs:
 | 
					 | 
				
			||||||
        for datum in data:
 | 
					 | 
				
			||||||
            if datum["key"] in paragraph.text:
 | 
					 | 
				
			||||||
                if "text" == datum["type"]:
 | 
					 | 
				
			||||||
                    paragraph.text = paragraph.text.replace(datum["key"], datum["value"])
 | 
					 | 
				
			||||||
                elif "picture" == datum["type"]:
 | 
					 | 
				
			||||||
                    paragraph.clear()
 | 
					 | 
				
			||||||
                    paragraph.add_run().add_picture(datum["value"], width=Inches(datum["width"]))
 | 
					 | 
				
			||||||
                elif "number" == datum["type"]:
 | 
					 | 
				
			||||||
                    paragraph.text = paragraph.text.replace(datum["key"], str(datum["value"]))
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    # 表格替换
 | 
					 | 
				
			||||||
    for table in doc.tables:
 | 
					 | 
				
			||||||
        for row in table.rows:
 | 
					 | 
				
			||||||
            for cell in row.cells:
 | 
					 | 
				
			||||||
                for datum in data:
 | 
					 | 
				
			||||||
                    if datum["key"] in cell.text:
 | 
					 | 
				
			||||||
                        if "text" == datum["type"]:
 | 
					 | 
				
			||||||
                            cell.text = cell.text.replace(datum["key"], datum["value"])
 | 
					 | 
				
			||||||
                        elif "picture" == datum["type"]:
 | 
					 | 
				
			||||||
                            paragraph = cell.paragraphs[0]
 | 
					 | 
				
			||||||
                            paragraph.clear()
 | 
					 | 
				
			||||||
                            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"]))
 | 
					 | 
				
			||||||
    doc.save(filename)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
@ -1 +1,6 @@
 | 
				
			|||||||
# 读取模板生成报表
 | 
					# 读取模板生成报表
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					```python
 | 
				
			||||||
 | 
					pyinstaller -F main.py -w
 | 
				
			||||||
 | 
					```
 | 
				
			||||||
					Loading…
					
					
				
		Reference in new issue