Python之xml的特殊处理

288 阅读1分钟

功能介绍

把xml里的style属性内部的值提取到外面。

安装模块

pip install cssutils

上面的库是css解析库。

实现功能

import cssutils
from xml.dom import minidom


def update_xml(file_xml):
    dom = minidom.parse(file_xml)
    root = dom.documentElement
    root = extract_change_style(root)
    with open(file_xml, 'w', encoding='utf-8') as f:
        f.write(root.toxml())
    return file_xml


def extract_change_style(node):
    for i in range(0, node.attributes.length):
        attr = node.attributes.item(i)
        attr_name = attr.name
        if attr_name == 'style':
            attr_value = attr.value
            sheet = cssutils.parseString("#rule{" + attr_value + "}")

            for rule in sheet.cssRules:
                if rule.type == rule.STYLE_RULE:
                    for property in rule.style:
                        if property.name in ['fill', 'stroke', 'stop-color']:
                            if not property.value in ['none', 'transport']:
                                node.setAttribute(property.name, property.value)
                                rule.style.removeProperty(property.name)

            text = sheet.cssText
            text = text.decode('utf-8').replace("#rule {", "").replace("}", "").replace("\n", "").replace("\r\n",
                                                                                                          "").replace(
                "    ", "")
            node.setAttribute("style", text)

    for i, child in enumerate(node.childNodes):
        if child.nodeType == 1:
            temp = extract_change_style(child)
            node.childNodes[i] = temp
    return node

if __name__ == '__main__':
    file_xml = 'demo.xml'
    file_xml_path = update_xml(file_xml=file_xml)
    print(file_xml_path)