python-frontmatter

Parse and manage posts with YAML (or other) frontmatter

23 个版本 Python >=3.10
Chris Amico <Chris Amico <eyeseast@gmail.com>>
安装
pip install python-frontmatter
poetry add python-frontmatter
pipenv install python-frontmatter
conda install python-frontmatter
描述

Python Frontmatter

Front matter offers a useful way to add arbitrary, structured metadata to text documents, regardless of type.

This is a small package to load and parse files (or just text) with YAML (or JSON, TOML or other) front matter.

Tests PyPI

Documentation

Install:

# with pip
pip install python-frontmatter

# or uv
uv add python-frontmatter

# local development, with uv
uv sync

Usage:

>>> import frontmatter

Load a post from a filename:

>>> post = frontmatter.load('tests/yaml/hello-world.txt')

Or a file (or file-like object):

>>> with open('tests/yaml/hello-world.txt') as f:
...     post = frontmatter.load(f)

Or load from text:

>>> with open('tests/yaml/hello-world.txt') as f:
...     post = frontmatter.loads(f.read())

If the file has a Byte-Order Mark (BOM), strip it off first. An easy way to do this is by using the utf-8-sig encoding:

>>> with open('tests/yaml/hello-world.txt', encoding="utf-8-sig") as f:
...     post = frontmatter.load(f)

Access content:

>>> print(post.content)
Well, hello there, world.

# this works, too
>>> print(post)
Well, hello there, world.

Use metadata (metadata gets proxied as post keys):

>>> print(post['title'])
Hello, world!

Metadata is a dictionary, with some handy proxies:

>>> sorted(post.keys())
['layout', 'title']

>>> from pprint import pprint
>>> post['excerpt'] = 'tl;dr'
>>> pprint(post.metadata)
{'excerpt': 'tl;dr', 'layout': 'post', 'title': 'Hello, world!'}

If you don't need the whole post object, just parse:

>>> with open('tests/yaml/hello-world.txt') as f:
...     metadata, content = frontmatter.parse(f.read())
>>> print(metadata['title'])
Hello, world!

Write back to plain text, too:

>>> print(frontmatter.dumps(post)) # doctest: +NORMALIZE_WHITESPACE
---
excerpt: tl;dr
layout: post
title: Hello, world!
---
Well, hello there, world.

Or write to a file (or file-like object):

>>> from io import StringIO
>>> f = StringIO()
>>> frontmatter.dump(post, f)
>>> print(f.getvalue()) # doctest: +NORMALIZE_WHITESPACE
---
excerpt: tl;dr
layout: post
title: Hello, world!
---
Well, hello there, world.

For more examples, see files in the tests/ directory. Each sample file has a corresponding .result.json file showing the expected parsed output. See also the examples/ directory, which covers more ways to customize input and output.

版本列表
1.3.0 2026-05-20
1.2.0 2026-05-17
1.1.0 2024-01-16
1.0.1 2023-11-11
1.0.0 2021-03-15
0.5.0 2019-11-25
0.4.5 2019-01-10
0.4.4 2018-11-12
0.4.3 2018-04-23
0.4.2 2017-03-25
0.4.1 2017-03-08
0.4.0 2017-03-04
0.3.1 2016-04-14
0.3.0 2016-04-14
0.2.1 2015-02-03
0.2.0 2015-02-02
0.1.6 2015-01-04
0.1.5 2014-12-02
0.1.4 2014-11-12
0.1.3 2014-11-09
0.1.2 2014-11-08
0.1.1 2014-09-15
0.1.0 2014-09-15