Word处理控件Aspose.Words功能演示:在 Python 中将 HTML 转换为 PNG、JPEG、BMP、GIF 或 TIFF 图像

在 Python 中将 HTML 转换为 PNG、JPEG、BMP、GIF 或 TIFF 图像

将 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 图像:

  1. 首先,使用Document类加载 HTML 文件。
  2. 或者,使用ImageSaveOptions类对象指定图像保存选项。
  3. 接下来,循环浏览文档中的所有页面。
  4. 之后,使用extract_pages()方法提取每个页面。
  5. 最后,使用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 documentdoc = aw.Document("C:\Files\sample.html")# Specify image save options# Set save format as JPEGimageOptions = 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_resolutionimageOptions.horizontal_resolution = 72# Save the pages as JPGfor 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 转换为 JPG/JPEG 图像
在 Python 中将 HTML 转换为 PNG 图像

我们可以按照以下步骤将 HTML 文档转换为 PNG 图像:

  1. 首先,使用Document类加载 HTML 文件。
  2. 接下来,创建ImageSaveOptions类的实例。
  3. 然后,指定图像保存选项,例如image_brightnessimage_contrast
  4. 接下来,循环浏览文档中的所有页面。
  5. 之后,使用extract_pages()方法提取每个页面。
  6. 最后,使用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 documentdoc = aw.Document("C:\Files\sample.html")# Specify image save options# Set save format as PNGimageOptions = 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.3imageOptions.image_contrast = 0.7# Save the pages as PNGfor 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 图像:

  1. 首先,使用Document类加载 HTML 文件。
  2. 接下来,循环浏览文档中的所有页面。
  3. 之后,使用extract_pages()方法提取每个页面。
  4. 最后,使用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 documentdoc = aw.Document("C:\Files\sample.html")# Save the pages as BMPfor 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 documentdoc = aw.Document("C:\Files\sample.html")# Save the pages as GIFfor 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 图像:

  1. 使用Document类加载 HTML 文件。
  2. 使用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 documentdoc = aw.Document("C:\Files\sample.html")# Save the document as TIFFdoc.save(f"C:\Files\Images\Output.tiff")

在 Python 中将 HTML 转换为 TIFF 图像
Python中的HTML字符串到图像的转换

我们可以按照以下步骤从 HTML 字符串动态生成图像文件:

  1. 首先,创建Document类的实例。
  2. 接下来,创建DocumentBuilder类的实例。
  3. 之后,使用insert_html()方法插入 HTML 字符串。
  4. 最后,使用save()方法将文档保存为 JPG。

以下代码示例展示了如何在 Python 中将 HTML 字符串转换为 JPG 图像

# This code example demonstrates how to convert HTML string to an image.import aspose.words as aw# Create document objectdoc = aw.Document()# Create a document builder objectbuilder = aw.DocumentBuilder(doc)# Insert HTMLbuilder.insert_html("<ul>rn" +"<li>Item1</li>rn" +"<li>Item2</li>rn" +"</ul>")# Save the document as JPGdoc.save(f"C:\Files\Output.jpg")

在 Python 中将 HTML 字符串转换为图像
结论
  • 以编程方式将 HTML 文档转换为图像;
  • 将 HTML 文件的内容转换为 PNG、JPEG、BMP、GIF 或 TIFF 图像;
  • 生成 HTML 文档并使用 Python 将其转换为图像。

欢迎下载|体验更多Aspose产品

获取更多信息请咨询在线客服 或 加入Aspose技术交流群(
标签:

声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2022年6月23日
下一篇 2022年6月23日

相关推荐

发表回复

登录后才能评论