Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何将单词样式应用于Richtext对象?(docxtpl库)_Python_Xml_Python 2.7_Ms Word - Fatal编程技术网

Python 如何将单词样式应用于Richtext对象?(docxtpl库)

Python 如何将单词样式应用于Richtext对象?(docxtpl库),python,xml,python-2.7,ms-word,Python,Xml,Python 2.7,Ms Word,我目前正在尝试使用docxtpl生成word文档。但是,当样式名称包含空格时,我无法确定如何将word样式添加到Richtext对象,因为该样式不会应用到word文档中。在使用单个单词命名样式的其他情况下,它可以正常工作。 这是我目前的代码: from bs4 import BeautifulSoup from docxtpl import DocxTemplate, RichText html = "<html><body><p><p>I am

我目前正在尝试使用docxtpl生成word文档。但是,当样式名称包含空格时,我无法确定如何将word样式添加到Richtext对象,因为该样式不会应用到word文档中。在使用单个单词命名样式的其他情况下,它可以正常工作。 这是我目前的代码:

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

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

从示例文件来看,似乎无法将HTML字符串直接转换为RichText()。您必须使用它们的类语法,而不是

下面是一个例子:

rt = RichText('an exemple of ')

rt.add('a rich text', style='myrichtextstyle')
rt.add(' with ')
rt.add('some italic', italic=True)
rt.add(' and ')
rt.add('some violet', color='#ff00ff')
rt.add(' and ')
rt.add('some striked', strike=True)
rt.add(' and ')
rt.add('some small', size=14)
rt.add(' or ')
rt.add('big', size=60)
rt.add(' text.')
rt.add(' Et voilà ! ')
rt.add('\n1st line')
rt.add('\n2nd line')
rt.add('\n3rd line')
rt.add('\n\n<cool>')

context = {
    'example' : rt,
}

tpl.render(context)
tpl.save('test_files/richtext.docx')
rt=RichText('
rt.add('a rich text',style='myrichtextstyle')
rt.add('with')
rt.add('some italic',italic=True)
rt.add('和')
rt.add('一些紫罗兰色',颜色='#ff00ff')
rt.add('和')
rt.add('some strike',strike=True)
rt.add('和')
rt.add(“一些小的”,尺寸=14)
rt.add('或')
rt.add('大',尺寸=60)
rt.add('文本')
rt.add('Et voilá!')
rt.add(“\n第一行”)
rt.add(“\n第二行”)
rt.add(“\n3行”)
rt.add('\n\n')
上下文={
“示例”:rt,
}
tpl.render(上下文)
tpl.save('test_files/richtext.docx'))
资料来源:

rt = RichText('an exemple of ')

rt.add('a rich text', style='myrichtextstyle')
rt.add(' with ')
rt.add('some italic', italic=True)
rt.add(' and ')
rt.add('some violet', color='#ff00ff')
rt.add(' and ')
rt.add('some striked', strike=True)
rt.add(' and ')
rt.add('some small', size=14)
rt.add(' or ')
rt.add('big', size=60)
rt.add(' text.')
rt.add(' Et voilà ! ')
rt.add('\n1st line')
rt.add('\n2nd line')
rt.add('\n3rd line')
rt.add('\n\n<cool>')

context = {
    'example' : rt,
}

tpl.render(context)
tpl.save('test_files/richtext.docx')