用自定义样式替换Word docx的标题

2024-05-26 21:51:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用panflute为pandoc编写一个python过滤器,将Markdown转换为Word文档。通常,pandoc会将降价标题转换为Word的内置样式,称为heading1、heading2等。但由于我必须使用Word模板的特殊性,我需要将Word中的所有降价标题更改为相应的自定义样式,例如Header level 1=>;Header1,level 2=>;Header2等

下面是我为测试过滤器而制作的一个快速示例标记文件:

# Heading 1

some text in a paragraph

## Heading 2

a little bit more text down below

基本上,我想把降价转换成我写的那样:

^{pr2}$

这样,当我跑的时候:

pandoc -S test_input.md -o test_output.docx --reference-docx ./custom_styles.docx --filter ./test_filter.py

生成的Word docx将使用适当的自定义样式。在

跟我来?在

总之,下面是我用panfleet编写的过滤器:

#! /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替换Markdown头。从另一端出来,好像根本没有过滤器。在

我不知道我做错了什么。在


Tags: test过滤器docmain样式filterlevelpandoc
1条回答
网友
1楼 · 发布于 2024-05-26 21:51:03

啊哈!终于自己解决了。在

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()

相关问题 更多 >

    热门问题