MarkdownHandler — Aspose.Cells FOSS for Python API Reference
aspose-cells-foss can export worksheet data as a GitHub-Flavored Markdown table via the MarkdownHandler class. A convenience method workbook.save_as_markdown(path) is also available directly on the Workbook class.
Package: aspose.cells_foss
from aspose.cells_foss import MarkdownHandler, MarkdownSaveOptions
from aspose.cells_foss import save_workbook_as_markdownMarkdownSaveOptions
Options that control how the worksheet is serialized to Markdown.
Constructor
MarkdownSaveOptions()Properties
| Property | Type | Default | Description |
|---|---|---|---|
encoding | str | 'utf-8' | Character encoding for the output file. |
worksheet_index | int | 0 | Zero-based index of the worksheet to export. |
include_worksheet_name | bool | False | When True, writes the worksheet name as an <h2> heading above the table. |
header_level | int | 2 | Heading level (1–6) used when include_worksheet_name is True. |
column_alignments | dict | {} | Maps zero-based column indices to alignment strings: 'left', 'center', or 'right'. |
default_alignment | str | 'left' | Alignment applied to columns not listed in column_alignments. |
date_format | str | — | Format string for datetime.date values. |
empty_cell_placeholder | str | '' | Text written for empty cells (e.g., '-' or 'N/A'). |
first_row_as_header | bool | True | When True, the first data row is rendered as the Markdown table header row. |
float_precision | int | -1 | Number of decimal places for float values. -1 uses Python’s default str() representation. |
skip_empty_rows | bool | False | When True, rows where every cell is empty are omitted from the output. |
MarkdownHandler
MarkdownHandler contains only static methods and is never instantiated.
MarkdownHandler.save_markdown
MarkdownHandler.save_markdown(workbook, file_path: str, options: MarkdownSaveOptions | None = None) -> NoneExports the worksheet specified by options.worksheet_index (default 0) to a Markdown file at file_path. If options is None, default MarkdownSaveOptions are used.
MarkdownHandler.save_markdown_to_string
MarkdownHandler.save_markdown_to_string(workbook, options: MarkdownSaveOptions | None = None) -> strSerializes the worksheet to a Markdown string and returns it. No file is written.
Module Function
save_workbook_as_markdown
save_workbook_as_markdown(workbook, file_path: str, options: MarkdownSaveOptions | None = None) -> NoneConvenience wrapper around MarkdownHandler.save_markdown(workbook, file_path, options).
Workbook Convenience Method
Workbook.save_as_markdown(path) is a shorthand for exporting the first worksheet with default options:
wb.save_as_markdown("output.md")Example
The following example builds a small product table and exports it to a Markdown string with center-aligned numeric columns.
from aspose.cells_foss import Workbook, MarkdownHandler, MarkdownSaveOptions
wb = Workbook()
ws = wb.worksheets[0]
ws.name = "Products"
# Write table data (first row becomes the Markdown header)
rows = [
["Product", "Unit Price", "In Stock"],
["Widget A", 9.99, True],
["Widget B", 24.50, False],
["Widget C", 4.75, True],
]
for r, row in enumerate(rows):
for c, val in enumerate(row):
ws.cells[r][c].put_value(val)
opts = MarkdownSaveOptions()
opts.first_row_as_header = True
opts.column_alignments = {1: "right", 2: "center"} # price right, stock center
opts.float_precision = 2
opts.empty_cell_placeholder = "-"
# Get the Markdown as a string
md_text = MarkdownHandler.save_markdown_to_string(wb, options=opts)
print(md_text)
# Also save to a file
MarkdownHandler.save_markdown(wb, "products.md", options=opts)Output (approximate):
| Product | Unit Price | In Stock |
|----------|----------:|:--------:|
| Widget A | 9.99 | True |
| Widget B | 24.50 | False |
| Widget C | 4.75 | True |