python-docx richtext_如何将文字样式应用于Richtext对象?(docxtpl库)

我正在尝试使用docxtpl生成word文档。但是,当样式名包含空格时,我无法解决如何将word样式添加到Richtext对象中,因为样式不会在word文档中应用。在其他情况下,如果样式是用一个单词命名的,则效果很好。

以下是我当前的代码:from bs4 import BeautifulSoup

from docxtpl import DocxTemplate, RichText

html = "

I am a paragraph generated by python.

  • List 1 item 1
  • List 1 item " \

    "2

Example below:

List 2 item 1List 2 item 1

End paragraph

"

def main():

soup = BeautifulSoup(html, 'html.parser').find_all()

rt = RichText()

for tag in soup:

if tag.name == 'p' and tag.parent.name != 'p':

print tag.text

rt.add(tag.text + "\n\n")

elif tag.name == 'li' and tag.parent.name != 'li':

rt.add(tag.text + "\n", style='Subtle Reference')

output_data = {"data": rt}

tpl = DocxTemplate('template.docx')

tpl.render(output_data)

tpl.save('output.docx')

word文档中的Jinja代码:

^{pr2}$

我在gitlab上创建了一个问题,但是想知道是否有人以前使用过这个库,并且有什么好的解决方法?在

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值