Word处理控件Aspose.Words功能演示:在 Python 中将 HTML 转换为 PNG、JPEG、BMP、GIF 或 TIFF 图像
Aspose API支持流行文件格式处理,并允许将各类文档导出或转换为固定布局文件格式和最常用的图像/多媒体格式。
HTML (超文本标记语言)是所有浏览器都支持的主要网页文件格式。它经常用于将数据和信息显示为网页。在某些情况下,我们可能需要将 HTML 文档转换为JPG、PNG、TIFF、BMP、GIF等图像格式。在本文中,我们将学习如何将 HTML 转换为 PNG、JPEG、BMP、GIF、或 Python 中的 TIFF 图像。
Aspose.Words for.NET 最新下载(qun:761297826)https://www.evget.com/product/564/download
将 HTML 转换为图像的 Python API
为了将 HTML 转换为图像格式,我们将使用Aspose.Words for Python API。它是在 Python 应用程序中以编程方式读取和操作各种类型文档的完整解决方案。它使我们能够生成、修改、转换、渲染和打印 Microsoft Word(DOC、DOCX、ODT)、PDF和 Web(HTML、Markdown)文档。
请在控制台中使用以下 pip 命令从PyPI安装 API :
> pip install aspose-words
在 Python 中将 HTML 转换为 JPG 图像
我们可以按照以下步骤轻松地将 HTML 文档转换为 JPG 图像:
- 首先,使用Document类加载 HTML 文件。
- 或者,使用ImageSaveOptions类对象指定图像保存选项。
- 接下来,循环浏览文档中的所有页面。
- 之后,使用extract_pages()方法提取每个页面。
- 最后,使用save()方法将页面保存为 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)
在 Python 中将 HTML 转换为 PNG 图像
我们可以按照以下步骤将 HTML 文档转换为 PNG 图像:
- 首先,使用Document类加载 HTML 文件。
- 接下来,创建ImageSaveOptions类的实例。
- 然后,指定图像保存选项,例如image_brightness、image_contrast。
- 接下来,循环浏览文档中的所有页面。
- 之后,使用extract_pages()方法提取每个页面。
- 最后,使用save()方法将页面保存为 JPG。
以下代码示例展示了如何在 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)
Python中的HTML到BMP转换
我们可以按照以下步骤将 HTML 文档转换为 BMP 图像:
- 首先,使用Document类加载 HTML 文件。
- 接下来,循环浏览文档中的所有页面。
- 之后,使用extract_pages()方法提取每个页面。
- 最后,使用save()方法将页面保存为 JPG。
以下代码示例展示了如何在 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")
在 Python 中将 HTML 转换为 GIF 图像
同样,我们也可以按照前面提到的步骤将 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")
在 Python 中将 HTML 转换为 TIFF 图像
我们还可以按照以下步骤将 HTML 文档转换为 TIFF 图像:
我们还可以按照以下步骤将 HTML 文档转换为 TIFF 图像:
- 使用Document类加载 HTML 文件。
- 使用save()方法将文档保存为 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")
Python中的HTML字符串到图像的转换
我们可以按照以下步骤从 HTML 字符串动态生成图像文件:
- 首先,创建Document类的实例。
- 接下来,创建DocumentBuilder类的实例。
- 之后,使用insert_html()方法插入 HTML 字符串。
- 最后,使用save()方法将文档保存为 JPG。
以下代码示例展示了如何在 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")
结论
在本文中,我们学习了如何:
- 以编程方式将 HTML 文档转换为图像;
- 将 HTML 文件的内容转换为 PNG、JPEG、BMP、GIF 或 TIFF 图像;
- 生成 HTML 文档并使用 Python 将其转换为图像。
相关文章
- 【Python】python 日期操作
- python 时间与时间戳之间的转换
- python正常时间和unix时间戳相互转换的方法
- python数据结构之二叉树的统计与转换实例
- python—时间与时间戳之间的转换
- python常用代码
- python实现语音录入识别
- 【Python五篇慢慢弹(5)】类的继承案例解析,python相关知识延伸
- 用python实现简单EXCEL数据统计的实例
- Python 日期和时间_python 当前日期时间_python日期格式化
- Python每日一练(20230226)
- Python语言学习之打印输出那些事:python输出图表和各种吊炸天的字符串或图画、版权声明(如README.md)等之详细攻略
- Python语言学习:Python语言学习之python包/库package的简介(模块的封装/模块路径搜索/模块导入方法/自定义导入模块实现华氏-摄氏温度转换案例应用)、使用方法、管理工具之详细攻略
- Python编程语言学习:python编程语言中重要函数讲解之map函数等简介、使用方法之详细攻略
- Python之youtube_dl:youtube_dl库的简介、安装、使用方法之详细攻略
- Python:利用python代码编程实现将视频的avi格式转换为MP4格式
- 细说Python Lambda函数的用法,建议收藏!
- 从零开始学python | 什么是Python JSON?
- 4行Python代码生成图像验证码
- 【Python成长之路】python 基础篇 -- global/nonlocal关键字使用
- Python学习18:函数的参数
- Python并发请求之requests_future模块使用
- Python: ljust()|rjust()|center()字符串对齐
- Python - 缩写(capwords) 和 创建转换表(maketrans) 详细说明
- python chunk 方式读取大文件——本质上还是file read自身支持
- redis sentinel 高可用(HA)方案部署,及python应用示例
- Python数模笔记-StatsModels 统计回归(1)简介
- Python 代码推送百度链接
- Python:判断可逆素数