Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Last active July 31, 2023 19:35
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aspose-com-gists/2741bb268101fd3e5ec74a860abf717b to your computer and use it in GitHub Desktop.
Save aspose-com-gists/2741bb268101fd3e5ec74a860abf717b to your computer and use it in GitHub Desktop.
Convert HTML to Image in Python | HTML to JPG in Python | HTML to PNG in Python | HTML to TIFF in Python

Learn how to convert HTML to PNG, JPEG, BMP, GIF, or TIFF images in Python : https://blog.aspose.com/2022/06/20/convert-html-to-image-in-python/

The following topics are covered in this article:

  1. Python API to Convert HTML to Image
  2. Convert HTML to JPG Image
  3. Convert HTML to PNG Image
  4. HTML to BMP Conversion
  5. Convert HTML to GIF Image
  6. Convert HTML to TIFF Image
  7. HTML String to Image Conversion
# This code example demonstrates how to convert HTML document to BMP images.
import aspose.words as aw
# Load an existing Word document
doc = aw.Document("C:\\Files\\sample.html")
# Save the pages as BMP
for page in range(0, doc.page_count):
extractedPage = doc.extract_pages(page, 1)
extractedPage.save(f"C:\\Files\\Images\\Page_{page + 1}.bmp")
# This code example demonstrates how to convert HTML document to GIF images.
import aspose.words as aw
# Load an existing Word document
doc = aw.Document("C:\\Files\\sample.html")
# Save the pages as GIF
for page in range(0, doc.page_count):
extractedPage = doc.extract_pages(page, 1)
extractedPage.save(f"C:\\Files\\Images\\Page_{page + 1}.gif")
# This code example demonstrates how to convert HTML document to JPG images.
import aspose.words as aw
# Load an existing Word document
doc = aw.Document("C:\\Files\\sample.html")
# Specify image save options
# Set save format as JPEG
imageOptions = aw.saving.ImageSaveOptions(aw.SaveFormat.JPEG)
# Set the "JpegQuality" property to "10" to use stronger compression when rendering the document.
# This will reduce the file size of the document, but the image will display more prominent compression artifacts.
imageOptions.jpeg_quality = 10
# Change the horizontal resolution.
# The default value for these properties is 96.0, for a resolution of 96dpi.
# Similarly, change vertical resolution by setting vertical_resolution
imageOptions.horizontal_resolution = 72
# Save the pages as JPG
for page in range(0, doc.page_count):
extractedPage = doc.extract_pages(page, 1)
extractedPage.save(f"C:\\Files\\Images\\Page_{page + 1}.jpg", imageOptions)
# This code example demonstrates how to convert HTML document to PNG images.
import aspose.words as aw
# Load an existing Word document
doc = aw.Document("C:\\Files\\sample.html")
# Specify image save options
# Set save format as PNG
imageOptions = aw.saving.ImageSaveOptions(aw.SaveFormat.PNG)
# Change the image's brightness and contrast.
# Both are on a 0-1 scale and are at 0.5 by default.
imageOptions.image_brightness = 0.3
imageOptions.image_contrast = 0.7
# Save the pages as PNG
for page in range(0, doc.page_count):
extractedPage = doc.extract_pages(page, 1)
extractedPage.save(f"C:\\Files\\Images\\Page_{page + 1}.png", imageOptions)
# This code example demonstrates how to convert HTML document to TIFF images.
import aspose.words as aw
# Load an existing Word document
doc = aw.Document("C:\\Files\\sample.html")
# Save the document as TIFF
doc.save(f"C:\\Files\\Images\\Output.tiff")
# This code example demonstrates how to convert HTML string to an image.
import aspose.words as aw
# Create document object
doc = aw.Document()
# Create a document builder object
builder = aw.DocumentBuilder(doc)
# Insert HTML
builder.insert_html("<ul>\r\n" +
"<li>Item1</li>\r\n" +
"<li>Item2</li>\r\n" +
"</ul>")
# Save the document as JPG
doc.save(f"C:\\Files\\Output.jpg")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment