Untitled

 avatar
unknown
python
2 years ago
1.9 kB
5
Indexable
import os
import xml.etree.ElementTree as ET

import docx


def parse_to_file(xml_text, file_name):
    root = ET.fromstring(xml_text)

    doc = docx.Document()
    table = doc.add_table(rows=1, cols=3)
    header_cells = table.rows[0].cells
    header_cells[0].text = "Поле"
    header_cells[1].text = "Тип"
    header_cells[2].text = "Документация"

    result = {}
    for elem in root.findall('.//{http://www.w3.org/2001/XMLSchema}element'):
        name = elem.get('name')
        _type = elem.get('type')
        if _type is not None:
            _type = _type.replace("xsd:", "")
        if elem.find('.//{http://www.w3.org/2001/XMLSchema}documentation') is None:
            continue
        documentation = elem.find('.//{http://www.w3.org/2001/XMLSchema}documentation').text.strip()
        result[name] = (_type, documentation)

    for name, (_type, documentation) in result.items():
        row_cells = table.add_row().cells
        if name is None:
            name = ""
        if _type is None:
            _type = ""
        if documentation is None:
            documentation = ""
        row_cells[0].text = name
        row_cells[1].text = _type
        row_cells[2].text = documentation

    doc.save(file_name)


if __name__ == '__main__':
    # меняешь path на твою папку и все
    path = "C:\\Users\\Myskill.PC\\PycharmProjects\\fastapi-jwt\\xsds"
    dir_list = os.listdir(path)

    print("Files and directories in '", path, "' :")

    for xsd_file in dir_list:
        if xsd_file.endswith("xsd"):
            with open(path + "\\" + xsd_file, 'r', encoding='utf-8') as file:
                data = file.read()

            docx_name = xsd_file.replace("xsd", "docx")
            parse_to_file(data, path + "\\" + docx_name)
Editor is loading...