Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
如果您需要预览 Markdown 文件,您可以将其转换为图像格式。使用 Aspose.HTML for Python via .NET,您只需几行代码就能轻松地将 Markdown 转换为 JPG、PNG、BMP、GIF 或 TIFF 文件!
本文介绍了如何使用 Converter 类将 Markdown 转换为图像格式。您将了解支持的转换场景,并考虑用 Python 代码示例来说明这些场景。此外,您还可以试用在线 Markdown 转换器来测试 Aspose.HTML API 的功能并即时转换 Markdown。
注: 转换器类的所有 convert_markdown() 方法都允许将 Markdown 转换为 HTML。从 Markdown 到其他格式的转换需要经过 Markdown 到 HTML 的转换阶段。
您可以使用 Aspose.HTML 将 Markdown 实时转换为其他格式。请从本地文件系统或 URL 加载 Markdown,选择输出格式并运行示例。 保存选项为默认设置。您将立即以单独文件的形式收到转换结果。
如果您想通过编程将 Markdown 转换为图像格式,请参阅以下 Python 代码示例。
从 Markdown 到其他格式的转换需要经过 Markdown 到 HTML 的转换阶段。要将 Markdown 转换为 PNG,应遵循以下几个步骤:
HTMLDocument、ImageSaveOptions 和输出文件路径 save_path 传递给 convert_html() 方法,以便将 HTML 转换为 PNG。如果您要在代码中直接从用户字符串创建 Markdown 文档,并将其转换为 PNG 图像,下面的示例可以帮到您:
1# Convert Markdown to PNG using Python
2
3import os
4import aspose.html.converters as conv
5import aspose.html.saving as sav
6
7# Setup output directory and paths
8output_dir = "output/"
9os.makedirs(output_dir, exist_ok=True)
10source_path = os.path.join(output_dir, "document.md")
11
12# Create a simple Markdown example file
13code = "### Hello, World!\nConvert Markdown to PNG!"
14with open(source_path, "w") as file:
15 file.write(code)
16
17# Convert Markdown file to an intermediate HTMLDocument
18document = conv.Converter.convert_markdown(source_path)
19
20# Create ImageSaveOptions with PNG format
21options = sav.ImageSaveOptions()
22
23# Prepare output file path
24save_path = os.path.join(output_dir, "markdown-to-image.png")
25
26# Convert HTMLDocument to PNG image
27conv.Converter.convert_html(document, options, save_path)Aspose.HTML 提供免费的在线 MD to PNG Converter,可将 Markdown 转换为高质量的 PNG 图像,简单快捷。只需上传、转换您的文件,几秒钟就能得到结果!
将 Markdown 转换为图片的过程可以灵活定制。ImageSaveOptions 类为将 HTML 内容转换为图片格式提供了广泛的自定义功能。以下是对 ImageSaveOptions 各属性的详细说明:
CssOptions 表示,允许配置在 HTML 转换为图像时如何处理 CSS 属性。如果你想使用自定义保存选项从本地文件系统转换现有的 Markdown 文档,下面的示例可以帮到你:
1# Convert Markdown to JPG using Python with custom settings
2
3import os
4import aspose.html.converters as conv
5import aspose.html.saving as sav
6import aspose.html.drawing as dr
7import aspose.html.rendering.image as rim
8
9# Setup directories and define paths
10output_dir = "output/"
11input_dir = "data/"
12if not os.path.exists(output_dir):
13 os.makedirs(output_dir)
14document_path = os.path.join(input_dir, "document.md")
15save_path = os.path.join(output_dir, "md-to-jpg-with-save-options.jpg")
16
17# Convert Markdown to HTML
18document = conv.Converter.convert_markdown(document_path)
19
20# Create an instance of ImageSaveOptions
21options = sav.ImageSaveOptions(rim.ImageFormat.JPEG)
22options.horizontal_resolution = dr.Resolution.from_dots_per_inch(200.0)
23options.vertical_resolution = dr.Resolution.from_dots_per_inch(200.0)
24options.css.media_type.PRINT
25
26# Convert HTML to JPG
27conv.Converter.convert_html(document, options, save_path)Aspose.HTML for Python via .NET 支持将 Markdown 转换为 PNG、JPG、JPEG、BMP、TIFF 和 GIF 图像。您可以使用上述 Python 代码来实现这一功能;要设置输出图像格式,您只需在输出文件名中指定所需的扩展名(格式),并设置保存选项对象的 format 属性。
例如,要将 Markdown 转换为 BMP,您需要
options = sav.ImageSaveOptions(rim.ImageFormat.BMP).bmp:save_path = os.path.join(output_dir, "markdown-to-image.bmp")下载 Aspose.HTML for Python via .NET库,它能让您成功、快速、轻松地将 HTML、MHTML、EPUB、SVG 和 Markdown 文档转换为最流行的格式。
您可以使用我们的在线 MD 到 JPG 转换器 检查 Markdown 转换为 JPG 的质量。上传、转换文件并在几秒钟内获得结果。现在就免费试用我们强大的 Markdown 转 JPG 转换器!
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.