Quickstart
Quickstart
설치 후 5분 만에 스타일이 적용된 문서 만들기: 이 페이지는 작은 DOM을 구축하고, 스타일시트를 적용한 뒤, 해결된 스타일을 다시 읽어옵니다.
Prerequisites
| Requirement | Detail |
|---|---|
| Python | 3.10 이상 |
| Package | aspose-html-foss 설치됨 (Installation) |
1. 문서와 요소 만들기
Document은 모든 노드 유형에 대한 팩토리입니다. 요소를 생성하고, 속성을 부여한 뒤 트리에 연결합니다:
from aspose_html.dom import Document
doc = Document()
el = doc.create_element("div")
el.set_attribute("class", "foo")
el.set_attribute("id", "bar")
doc.append_child(el)2. 스타일시트 연결
CSS 텍스트를 CSSStyleSheet.replace_sync()으로 파싱하고, 시트를 문서에 등록하여 캐스케이드에 참여하도록 합니다:
from aspose_html.cssom import CSSStyleSheet
sheet = CSSStyleSheet()
sheet.replace_sync(".foo { color: red } #bar { color: blue }")
doc.attach_style_sheet(sheet)3. 계산된 스타일 읽기
Element.get_computed_style()는 특이성, 중요도 및 상속을 해결합니다. 여기서 ID 선택자가 클래스 선택자보다 우선합니다:
style = el.get_computed_style()
print(style.get_property_value("color"))4. 인라인 스타일 시도
요소의 style 선언을 통해 설정된 인라인 선언은 작성자 스타일시트 규칙보다 우선합니다:
inline = el.style
inline.set_property("color", "green")
print(el.get_computed_style().get_property_value("color"))