翻譯|使用教程|編輯:胡濤|2022-07-26 11:10:16.733|閱讀 281 次
概述:本文主要介紹了如何在 Python 中將 HTML 轉換為 PNG、JPEG、BMP、GIF 或 TIFF 圖像,歡迎查閱!
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
HTML (超文本標記語言)是所有瀏覽器都支持的主要網頁文件格式。它經常用于將數據和信息顯示為網頁。在某些情況下,我們可能需要將 HTML 文檔轉換為JPG、PNG、TIFF、BMP、GIF等圖像格式。在本文中,我們將學習如何將 HTML 轉換為 PNG、JPEG、BMP、GIF、或 Python 中的 TIFF 圖像。
為了將 HTML 轉換為圖像格式,我們將使用Aspose.Words for Python API。它是在 Python 應用程序中以編程方式讀取和操作各種類型文檔的完整解決方案。它使我們能夠生成、修改、轉換、渲染和打印 Microsoft Word(DOC、DOCX、ODT)、PDF和 Web(HTML、Markdown)文檔。
請在控制臺中使用以下 pip 命令從PyPI安裝 API :
> pip install aspose-words
我們可以按照以下步驟輕松地將 HTML 文檔轉換為 JPG 圖像:
以下代碼示例展示了如何在 Python 中將 HTML 轉換為 JPG 圖像。
# 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)
我們可以按照以下步驟將 HTML 文檔轉換為 PNG 圖像:
以下代碼示例展示了如何在 Python 中將 HTML 轉換為 PNG 圖像。
# 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)
我們可以按照以下步驟將 HTML 文檔轉換為 BMP 圖像:
以下代碼示例展示了如何在 Python 中將 HTML 轉換為 BMP 圖像。
# 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")
同樣,我們也可以按照前面提到的步驟將 HTML 文檔轉換為 GIF 圖像。但是,我們只需要在步驟 4 中將圖像保存為帶有“.gif”擴展名的 GIF。
以下代碼示例展示了如何在 Python 中將 HTML 轉換為 GIF 圖像。
# 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")
我們還可以按照以下步驟將 HTML 文檔轉換為 TIFF 圖像:
我們還可以按照以下步驟將 HTML 文檔轉換為 TIFF 圖像:
以下代碼示例展示了如何在 Python 中將 HTML 文檔轉換為 TIFF 圖像。
# 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")
我們可以按照以下步驟從 HTML 字符串動態生成圖像文件:
以下代碼示例展示了如何在 Python 中將 HTML 字符串轉換為 JPG 圖像。
# 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")
在本文中,我們學習了如何:
歡迎下載|體驗更多Aspose產品
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn