import xml.etree.ElementTree as ET import glob from docx import Document def unify_response(input_str): input_str = input_str.lower() if input_str in ['yes', 'true']: return 'YES' else: return "NO" # 解析XML文件 xml_files = glob.glob('.\9-datastructure\*.xml') #xml_files = [".\9-datastructure\dsp_service.xml"] doc = Document('temp.docx') head_lines = ["字段", "名称", "数据类型", "主键", "非空", "外键", "备注"] for xml_file in xml_files: print("处理文件:" + xml_file) tree = ET.parse(xml_file) root = tree.getroot() database = root.find('database') doc.add_heading(database.get('name'), 2) doc.add_paragraph("表:" + database.get('name'), style='tablename') tables = root.findall(".//table") doc_table = doc.add_table(rows=1, cols=2, style="table1") row = doc_table.rows[0].cells row[0].text = "id" row[1].text = "name" for table in tables: row = doc_table.add_row().cells row[0].text = table.get("id") row[1].text = table.get("name") for table in tables: doc.add_paragraph("表:" + table.get("name") + "(" + table.get("id") + ")列清单", style='tablename') # 初始化表头 doc_table = doc.add_table(rows=1, cols=7, style="table1") i = 0 for head_line in head_lines: doc_table.rows[0].cells[i].text = head_line i += 1 # 插数据 for column in table: row = doc_table.add_row().cells row[0].text = column.attrib['id'] if "name" in column.attrib: row[1].text = column.attrib['name'] if "size" in column.attrib: row[2].text = column.attrib['type'] + "(" + column.attrib['size'] + ")" else: row[2].text = column.attrib['type'] if "name" in column.attrib and "主键" in column.attrib['name']: row[3].text = "PRI" row[4].text = unify_response(column.attrib['required']) row[5].text = "NO" style = doc.styles['table_raw'] for table in doc.tables: for row in table.rows: for cell in row.cells: for paragraph in cell.paragraphs: paragraph.style = style doc.save("out.docx")