zl程序教程

您现在的位置是:首页 >  后端

当前栏目

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

Python转换HTML 处理 功能 控件 图像 word
2023-09-11 14:14:49 时间

Aspose API支持流行文件格式处理,并允许将各类文档导出或转换为固定布局文件格式和最常用的图像/多媒体格式。

HTML (超文本标记语言)是所有浏览器都支持的主要网页文件格式。它经常用于将数据和信息显示为网页。在某些情况下,我们可能需要将 HTML 文档转换为JPG、PNG、TIFF、BMP、GIF等图像格式。在本文中,我们将学习如何将 HTML 转换为 PNG、JPEG、BMP、GIF、或 Python 中的 TIFF 图像。

Aspose.Words for.NET 最新下载(qun:761297826)icon-default.png?t=M85Bhttps://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 图像:

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

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

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

  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 document
doc = aw.Document("C:\\Files\\sample.html")

# Save the document as TIFF
doc.save(f"C:\\Files\\Images\\Output.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 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 将其转换为图像。