如何在python中创建CSS文件

如何在python中创建CSS文件,python,css,python-3.7,Python,Css,Python 3.7,如何在python中创建CSS文件 # -*- coding: utf-8 -*- import cssutils css = u'''/* a comment */ .chat { background: #fff; color: white; } .chat-history { height: 100px; padding: 8px 24px; overflow-y: scroll;

如何在python中创建CSS文件

# -*- coding: utf-8 -*-
import cssutils

css = u'''/* a comment */
    .chat {
        background: #fff;
        color: white;
    }

    .chat-history {
        height: 100px;
        padding: 8px 24px;
        overflow-y: scroll;
    }
#live-chat header {
    background: #293239;
    border-radius: 5px 5px 0 0;
    color: #fff;
    cursor: pointer;
    padding: 16px 24px;
}
'''
sheet = cssutils.parseString(css)

for rule in sheet:
    if rule.type == rule.STYLE_RULE:
        # find property
        for property in rule.style:
            if property.name == 'color':
                property.value = 'green'
                property.priority = 'IMPORTANT'
                break
        # or simply:
        rule.style['margin'] = '01.0eM' # or: ('1em', 'important')


# cssutils.ser.prefs.resolveVariables == True since 0.9.7b2
print(sheet.cssText)
with open("hello.css", 'w') as f:
    f.write(str(sheet.cssText))
当我翻阅我的档案时

b'/* a comment */\n.chat {\n    background: #fff;\n    color: green !important;\n    margin: 1em\n    }\n.chat-history {\n    height: 100px;\n    padding: 8px 24px;\n    overflow-y: scroll;\n    margin: 1em\n    }\n#live-chat header {\n    background: #293239;\n    border-radius: 5px 5px 0 0;\n    color: green !important;\n    cursor: pointer;\n    padding: 16px 24px;\n    margin: 1em\n    }'

我想创建一个缩进的CSS文件

在写入CSS文件之前,可以使用函数
sheet.cssText.decode('ASCII')
以ASCII格式解码
cssText

# -*- coding: utf-8 -*-
import cssutils

css = '''/* a comment */
    .chat {
        background: #fff;
        color: white;
    }

    .chat-history {
        height: 100px;
        padding: 8px 24px;
        overflow-y: scroll;
    }
#live-chat header {
    background: #293239;
    border-radius: 5px 5px 0 0;
    color: #fff;
    cursor: pointer;
    padding: 16px 24px;
}
'''
sheet = cssutils.parseString(css)

for rule in sheet:
    if rule.type == rule.STYLE_RULE:
        # find property
        for property in rule.style:
            if property.name == 'color':
                property.value = 'green'
                property.priority = 'IMPORTANT'
                break
        # or simply:
        rule.style['margin'] = '01.0eM' # or: ('1em', 'important')


# cssutils.ser.prefs.resolveVariables == True since 0.9.7b2
cssTextDecoded = sheet.cssText.decode('ascii')
print(cssTextDecoded)
with open("hello.css", 'w') as f:
    f.write(cssTextDecoded)

在写入CSS文件之前,您可以使用函数
sheet.cssText.decode('ASCII')
在ASCII中解码
cssText

# -*- coding: utf-8 -*-
import cssutils

css = '''/* a comment */
    .chat {
        background: #fff;
        color: white;
    }

    .chat-history {
        height: 100px;
        padding: 8px 24px;
        overflow-y: scroll;
    }
#live-chat header {
    background: #293239;
    border-radius: 5px 5px 0 0;
    color: #fff;
    cursor: pointer;
    padding: 16px 24px;
}
'''
sheet = cssutils.parseString(css)

for rule in sheet:
    if rule.type == rule.STYLE_RULE:
        # find property
        for property in rule.style:
            if property.name == 'color':
                property.value = 'green'
                property.priority = 'IMPORTANT'
                break
        # or simply:
        rule.style['margin'] = '01.0eM' # or: ('1em', 'important')


# cssutils.ser.prefs.resolveVariables == True since 0.9.7b2
cssTextDecoded = sheet.cssText.decode('ascii')
print(cssTextDecoded)
with open("hello.css", 'w') as f:
    f.write(cssTextDecoded)