Skip to content

Upgrade python-docx to 0.8.2. breaks script with KeyError: u"no style with name 'Light Shading Accent 1' #149

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
tooh opened this issue Feb 18, 2015 · 7 comments

Comments

@tooh
Copy link

tooh commented Feb 18, 2015

File "/Users/peter/.rvm/lib/python2.7/site-packages/docx/styles/styles.py", line 57, in __getitem__
    raise KeyError("no style with name '%s'" % key)
KeyError: u"no style with name 'Light Shading Accent 1'"

I suspect a localization related problem.

@scanny
Copy link
Contributor

scanny commented Feb 19, 2015

Hi Peter, I'll need to see the line of code that's triggering this along with a few lines of context.

Also I'll need to know if you're starting the document from the default or opening a "template" document of your own, i.e. Document() or Document('my_doc.docx').

I have a suspicion we'll have to change the default behavior upon adding a new table. It hadn't shown up before because it's only in this latest release that assigned style names are validated against styles actually available in the document.

@tooh
Copy link
Author

tooh commented Feb 19, 2015

This is the complete traceback

  1. this is the code that triggers it
Traceback (most recent call last):
  File "create_releasenotes.py", line 676, in <module>
    main()
  File "create_releasenotes.py", line 194, in main
    read_stories(url,file_name,template)
  File "create_releasenotes.py", line 358, in read_stories
    table_stories = document.add_table(1, 4)
  File "/Users/peter/.rvm/lib/python2.7/site-packages/docx/document.py", line 99, in add_table
    table.style = style
  File "/Users/peter/.rvm/lib/python2.7/site-packages/docx/table.py", line 130, in style
    style_or_name, WD_STYLE_TYPE.TABLE
  File "/Users/peter/.rvm/lib/python2.7/site-packages/docx/parts/document.py", line 75, in get_style_id
    return self.styles.get_style_id(style_or_name, style_type)
  File "/Users/peter/.rvm/lib/python2.7/site-packages/docx/styles/styles.py", line 113, in get_style_id
    return self._get_style_id_from_name(style_or_name, style_type)
  File "/Users/peter/.rvm/lib/python2.7/site-packages/docx/styles/styles.py", line 143, in _get_style_id_from_name
    return self._get_style_id_from_style(self[style_name], style_type)
  File "/Users/peter/.rvm/lib/python2.7/site-packages/docx/styles/styles.py", line 57, in __getitem__
    raise KeyError("no style with name '%s'" % key)
KeyError: u"no style with name 'Light Shading Accent 1'"

Nowhere in the script I use 'Light Shading Accent 1'. I know this is one of the built-ins.

  1. I start the document from a template document

I can look into this further coming weekend.

@scanny
Copy link
Contributor

scanny commented Feb 19, 2015

Ah, got it. It's as I suspected. This is a useful catch.

You can work around it by adding style=None to the document.add_table() call on line 358 of read_stories.

table_stories = document.add_table(1, 4, style=None)

I'm going to need to rethink using a default style on .add_table(). It escaped without notice before because there was no lookup to validate that a style existed in the document.

I'll get a patch out for this probably this weekend. Thanks for reporting it :)

@tooh
Copy link
Author

tooh commented Feb 19, 2015

I had several places where tables were created, so applied the workaround there.
Script is working again:

Have some rework to do for this 👍

/Users/peter/.rvm/lib/python2.7/site-packages/docx/styles/styles.py:54: UserWarning: style lookup by style_id is deprecated. Use style name as key instead.
  warn(msg, UserWarning)

@scanny
Copy link
Contributor

scanny commented Feb 19, 2015

On that one it's worth a try to use the style name just as it appears in the user interface (UI). Style lookup is now based on the style name rather than the style_id.

Based on the localized document you sent, it looks like the name for a custom (user created as opposed to built-in) style is assigned in the local language, with exactly the name you give it.

If that doesn't work, you may need to inspect the styles.xml with opc-diag to see the exact name it's using. But I think it's going to work. I don't know where it would get the spelling to use in the UI otherwise :)

If it's a built-in style, you'll need to use the English name for it. Inspecting the styles.xml part will give you that or there's a link in the docs here for a page where you can get them all translated for your localized language:http://python-docx.readthedocs.org/en/latest/user/styles-using.html

scanny pushed a commit that referenced this issue Feb 22, 2015
@scanny
Copy link
Contributor

scanny commented Feb 22, 2015

Fixed in release v0.8.5, pushed on Feb 21, 2015. Table style no longer defaults to 'Light Shading Accent 1'.

@scanny scanny closed this as completed Feb 22, 2015
@tooh
Copy link
Author

tooh commented Feb 22, 2015

Updated to v0.8.5. Seems OK. Still struggling with some table styles. Do not completely understand how table style name and id are handled. Will investigate further to describe it clearly.

styles = document.styles
                table_styles = [
                        s for s in styles if s.type == WD_STYLE_TYPE.TABLE
                ]
                for style in table_styles:
                        print(style.name)

Produces this list:

Normal Table
Light Grid1
Light Shading Accent 5
Table Grid
Light Shading
Light List
Light List Accent 1
Light List Accent 2
Light List Accent 3
Medium Shading 1 Accent 2
Medium Shading 1 Accent 1
Light List Accent 5

The style I'm actually using is not in this list.

How to prevent this warning is not so obvious to me.

/Users/peter/.rvm/lib/python2.7/site-packages/docx/styles/styles.py:54: UserWarning: style lookup by style_id is deprecated. Use style name as key instead.
  warn(msg, UserWarning)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants