Python pandoc-将Word docx的标题替换为自定义样式
我正在使用为pandoc编写一个python过滤器,以将Markdown转换为Word文档。通常,pandoc会将标记标题转换为Word的内置样式,称为标题1、标题2等。但由于我必须使用的Word模板的细节,我需要将所有标记标题更改为Word中相应的自定义样式,以便标题级别1=>Header1、级别2=>Header2等 下面是我为测试我的过滤器而制作的一个快速降价示例文件:Python pandoc-将Word docx的标题替换为自定义样式,python,pandoc,Python,Pandoc,我正在使用为pandoc编写一个python过滤器,以将Markdown转换为Word文档。通常,pandoc会将标记标题转换为Word的内置样式,称为标题1、标题2等。但由于我必须使用的Word模板的细节,我需要将所有标记标题更改为Word中相应的自定义样式,以便标题级别1=>Header1、级别2=>Header2等 下面是我为测试我的过滤器而制作的一个快速降价示例文件: # Heading 1 some text in a paragraph ## Heading 2 a littl
# Heading 1
some text in a paragraph
## Heading 2
a little bit more text down below
本质上,我想转换这个降价,就像我写的那样:
<div custom-style="Header1">Heading 1</div>
some text in a paragraph
<div custom-style="Header2">Heading 2</div>
a little bit more text down below
生成的单词docx将使用适当的自定义样式
跟着
总之,下面是我用Panflut编写的过滤器:
#! /usr/bin/env python
#coding: utf-8
from panflute import *
def action( elem, doc ):
if isinstance( elem, Header ):
return Div( elem, classes=['Header{}'.format(elem.level)] )
def main(doc=None):
return run_filter( action, doc=doc )
if __name__ == "__main__":
main()
不幸的是,它不能用我的自定义div替换标记标题来进行样式设置。它基本上从另一端出来,就好像根本没有过滤器一样
我不确定我做错了什么。啊哈!最后我自己解决了
from panflute import *
def action( elem, doc ):
if isinstance( elem, Header ):
#return Div( elem, attributes={'custom-style': 'Header{}'.format(elem.level)} )
return Div( Para(*elem.content), attributes={'custom-style': 'Header {}'.format(elem.level)} )
def main(doc=None):
return run_filter( action, doc=doc )
if __name__ == "__main__":
main()
我不熟悉Panfleet,可能会在pandoc讨论邮件列表上询问?
from panflute import *
def action( elem, doc ):
if isinstance( elem, Header ):
#return Div( elem, attributes={'custom-style': 'Header{}'.format(elem.level)} )
return Div( Para(*elem.content), attributes={'custom-style': 'Header {}'.format(elem.level)} )
def main(doc=None):
return run_filter( action, doc=doc )
if __name__ == "__main__":
main()