我正在尝试使用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 1End 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上创建了一个问题,但是想知道是否有人以前使用过这个库,并且有什么好的解决方法?在