1 回答

TA貢獻1847條經驗 獲得超7個贊
您可以在寫入 CSS 文件之前cssText使用該函數解碼in ASCIIsheet.cssText.decode('ascii')
# -*- 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)
添加回答
舉報