The conversion from HTML to image allows you to capture the appearance and layout of the HTML content as a static image file. It can be useful for various purposes, such as generating website previews, creating screenshots, archiving web pages, or integrating HTML content into applications that primarily deal with images. In this article, you will learn how to convert an HTML file or an HTML string to an image in Python using Spire.Doc for Python.
Install Spire.Doc for Python
This scenario requires Spire.Doc for Python and plum-dispatch v1.7.4. They can be easily installed in your Windows through the following pip command.
pip install Spire.Doc
If you are unsure how to install, please refer to this tutorial: How to Install Spire.Doc for Python on Windows
Convert an HTML File to an Image in Python
When an HTML file is loaded into the Document object using the Document.LoadFromFile() method, its contents are automatically rendered as the contents of a Word page. Then, a specific page can be saved as an image stream using the Document.SaveImageToStreams() method.
The following are the steps to convert an HTML file to an image with Python.
- Create a Document object.
- Load a HTML file using Document.LoadFromFile() method.
- Convert a specific page to an image stream using Document.SaveImageToStreams() method.
- Save the image stream as a PNG file using BufferedWriter.write() method.
- Python
from spire.doc import * from spire.doc.common import * # Create a Document object document = Document() # Load an HTML file document.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Sample.html", FileFormat.Html, XHTMLValidationType.none) # Save the first page as an image stream imageStream = document.SaveImageToStreams(0, ImageType.Bitmap) # Convert the image stream as a PNG file with open("output/HtmlToImage.png",'wb') as imageFile: imageFile.write(imageStream.ToArray()) document.Close()
Convert an HTML String to an Image in Python
To render uncomplicated HTML strings (typically text and its formatting) as a Word page, you can utilize the Paragraph.AppendHTML() method. Afterwards, you can convert it to an image stream using the Document.SaveImageToStreams() method.
The following are the steps to convert an HTML string to an image in Python.
- Create a Document object.
- Add a section using Document.AddSection() method.
- Add a paragraph using Section.AddParagraph() method.
- Specify the HTML string, and add the it to the paragraph using Paragraph.AppendHTML() method.
- Convert a specific page to an image stream using Document.SaveImageToStreams() method.
- Save the image stream as a PNG file using BufferedWriter.write() method.
- Python
from spire.doc import * from spire.doc.common import * # Create a Document object document = Document() # Add a section to the document sec = document.AddSection() # Add a paragraph to the section paragraph = sec.AddParagraph() # Specify the HTML string htmlString = """ <html> <head> <title>HTML to Word Example</title> <style> body { font-family: Arial, sans-serif; } h1 { color: #FF5733; font-size: 24px; margin-bottom: 20px; } p { color: #333333; font-size: 16px; margin-bottom: 10px; } ul { list-style-type: disc; margin-left: 20px; margin-bottom: 15px; } li { font-size: 14px; margin-bottom: 5px; } table { border-collapse: collapse; width: 100%; margin-bottom: 20px; } th, td { border: 1px solid #CCCCCC; padding: 8px; text-align: left; } th { background-color: #F2F2F2; font-weight: bold; } td { color: #0000FF; } </style> </head> <body> <h1>This is a Heading</h1> <p>This is a paragraph.</p> <p>Here's an unordered list:</p> <ul> <li>Item 1</li> <li>Item 2</li> <li>Item 3</li> </ul> <p>And here's a table:</p> <table> <tr> <th>Name</th> <th>Age</th> <th>Gender</th> </tr> <tr> <td>John Smith</td> <td>35</td> <td>Male</td> </tr> <tr> <td>Jenny Garcia</td> <td>27</td> <td>Female</td> </tr> </table> </body> </html> """ # Append the HTML string to the paragraph paragraph.AppendHTML(htmlString) # Save the first page as an image stream imageStream = document.SaveImageToStreams(0, ImageType.Bitmap) # Convert the image stream as a PNG file with open("output/HtmlToImage2.png",'wb') as imageFile: imageFile.write(imageStream.ToArray()) document.Close()
Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.