0
我目前正在尝试使用docxtpl生成word文档。但是,当样式名称包含空格时,我无法确定如何向Richtext对象添加单词样式,因为样式未在单词文档中应用。在使用单个单词命名样式的其他情况下,它可以正常工作。 这里是我当前的代码:如何将Word样式应用于Richtext对象? (docxtpl库)
from bs4 import BeautifulSoup
from docxtpl import DocxTemplate, RichText
html = "<html><body><p><p>I am a paragraph generated by python.</p></p><ul><li>List 1 item 1</li><li>List 1 item " \
"2</li></ul><p>Example below:</p><li>List 2 item 1</li><li>List 2 item 1</li><p>End paragraph</p></body></html> "
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文档中
神社代码:
{{r data}}
我创建与此gitlab的问题,但不知道有没有人使用这个库之前,不得不任何好的解决方法?