HTML 코드를 이미지로 변환하는 방법 (Html2Image)
들어가며
Html2Image
패키지를 이용하여 간단하게 HTML 코드를 이미지로 변환할 수 있다.

방법
Html2Image 패키지 설치하기
> pip install html2image
예제 코드
- 다음과 같이 3가지 방법으로 HTML 코드를 이미지 파일로 만들 수 있다.
- HTML 문자열(String) → 이미지
- HTML 파일(
.html
) → 이미지 - URL → 이미지
from html2image import Html2Image hti = Html2Image() html = '<h1> A title </h1> Some text.' css = 'body {background: red;}' # 1. screenshot an HTML string (css is optional) hti.screenshot(html_str=html, css_str=css, save_as='page.png') # 2. screenshot an HTML file hti.screenshot(html_file='page.html', css_file='style.css', save_as='page2.png') # 3. screenshot an URL hti.screenshot(url='https://www.python.org', save_as='python_org.png')
참고 사이트
HTML to IMAGE using Python
Here is a variable html_str, it is a string that contains html tags and contents in body. I am created a .html file from this string using the below code in python. html_file = open("filename.html...
stackoverflow.com
파이썬 이미지캡쳐 모듈 (Html2Image)
from flask import Flask, send_from_directory, send_file from html2image import Html2Image app = Flask(__name__) app.route("/") def hello_world(): url = 'https://www.python.org' width = 1200 height = 1024 filename = 'download_img.png' hti = Html2Image() #
gebal.tistory.com
html2image
Package acting as a wrapper around the headless mode of existing web browsers to generate images from URLs and from HTML+CSS strings or files.
pypi.org
'Programming > Python' 카테고리의 다른 글
[Python] 데이터 전처리 할 때 결측값 제거 방법 (Pandas) (1) | 2024.06.21 |
---|---|
[Python] or 연산자와 | 연산자의 차이 (0) | 2024.05.29 |
[Python] self (0) | 2023.11.29 |
[Python] ORM(Object Relational Mapping) 라이브러리 사용해보기 (SQLAlchemy) (0) | 2023.11.14 |
[Python] 파이썬을 이용하여 텔레그램(Telegram) 메시지 보내는 방법 (0) | 2023.11.06 |
[Python] 파이썬을 이용하여 이메일 보내는 방법 (smtplib, email) (0) | 2023.11.02 |
[Python] 파이썬에서 / 연산자와 // 연산자의 차이점 (0) | 2023.10.09 |
[Python] 맵(Map)과 리스트(List)의 차이점 (0) | 2023.10.08 |