MatthewP
MatthewP

Reputation: 79

Why MS-Word build-in styles not work while using python-docx?

I want to apply styles to my paragraph(List, List Number, List Number 2...etc.) using package python-docx. I create my own MS-Word template which has some specific styles I make. Also I sometimes need to apply MS-Word build-in style. Here's my code:

    elif to_U_sc:
        to_write = to_U_sc.group(2)
        paragraph = self.template.add_paragraph(to_write, style='List')
    elif to_O_sc2:
        to_write = to_O_sc2.group(2)
        paragraph = self.template.add_paragraph(to_write, style='ListNumber')

Here, self.template is Document object. The problem is no matter I use ListNumber or List Number or wdStyleListNumber as style name, all of them raise Error that no such style. Like:

File "/usr/lib/python2.7/site-packages/python_docx-0.8.5-py2.7.egg/docx/styles/styles.py", line 57, in __getitem__
    raise KeyError("no style with name '%s'" % key)
KeyError: u"no style with name 'ListNumber'"  

I try to print all styles in my template using code:

    def all_styles(self):
        styles = self._document.styles
        for style in styles:
            print('Name: {}\tType:{}'.format(style.name, style.type))

Here is the result:

Name: List Paragraph Type:PARAGRAPH (1)
Name: 浅色底纹 - 强调文字颜色 22 Type:TABLE (3)
Name: Light Shading Accent 2 Type:TABLE (3)
Name: 项目 Type:PARAGRAPH (1)
Name: 大结构 Type:PARAGRAPH (1)
Name: 标题 1 字符 Type:CHARACTER (2)
Name: 网格表 2 - 着色 21 Type:TABLE (3)
Name: 网格表 1 浅色 - 着色 21 Type:TABLE (3)
Name: 网格表 4 - 着色 51 Type:TABLE (3)
Name: 网格表 4 - 强调文字颜色 11 Type:TABLE (3)
Name: 列表段落 字符 Type:CHARACTER (2)
Name: footer Type:PARAGRAPH (1)
Name: 页脚 字符 Type:CHARACTER (2)
Name: Normal (Web) Type:PARAGRAPH (1)
Name: 浅色网格 - 强调文字颜色 11 Type:TABLE (3)
Name: 网格表 4 - 着色 41 Type:TABLE (3)
Name: 网格表 4 - 强调文字颜色 41 Type:TABLE (3)
Name: 网格表 4 - 着色 61 Type:TABLE (3)
Name: No Spacing Type:PARAGRAPH (1)
Name: 网格表 5 深色 - 着色 41 Type:TABLE (3)
Name: 网格表 7 彩色 - 着色 41 Type:TABLE (3)
Name: Balloon Text Type:PARAGRAPH (1)
Name: 批注框文本 字符 Type:CHARACTER (2)
Name: 标题 2 字符 Type:CHARACTER (2)
Name: 网格表 4 - 着色 5210 Type:TABLE (3)
Name: 网格表 4 - 着色 528 Type:TABLE (3)
Name: header Type:PARAGRAPH (1)
Name: 页眉 字符 Type:CHARACTER (2)
Name: TOC Heading Type:PARAGRAPH (1)
Name: toc 1 Type:PARAGRAPH (1)
Name: toc 2 Type:PARAGRAPH (1)
Name: toc 3 Type:PARAGRAPH (1)
Name: Hyperlink Type:CHARACTER (2)
Name: DetailTitle Type:PARAGRAPH (1)
Name: DetailTitle 字符 Type:CHARACTER (2)
Name: CDGene Type:TABLE (3)

Why so little styles?
I want to know How to apply such build-in style correctly.
Thanks!

Upvotes: 0

Views: 1562

Answers (1)

yoonghm
yoonghm

Reputation: 4625

See the description from the document.

In summary, if you create a new document via python-docx, it has limited styles. Use Microsoft Word to open the document and use only the available styles that suit your needs.

Alternatively, create an empty document using Microsoft Word. Customize and select the styles that you need. Use python-docx to use the styles that you have selected.

Upvotes: 1

Related Questions