python-docx style_id error while creating a word document

笑着哭i 提交于 2019-12-01 16:22:48

问题


I'm following the tutorial available on python-docx site to create a MS-Word document but I'm getting an error:

M:\Sites>python word.py
C:\Program Files\IBM\SPSS\Statistics\22\Python\lib\site-packages\docx\styles\sty
les.py:54: UserWarning: style lookup by style_id is deprecated. Use style name a
s key instead.
  warn(msg, UserWarning)

word.py

    from docx import Document
    from docx.shared import Inches
    import json

    document = Document()

    document.add_heading('Document Title', 0)

    p = document.add_paragraph('A plain paragraph having some ')
    p.add_run('bold').bold = True
    p.add_run(' and some ')
    p.add_run('italic.').italic = True

    document.add_heading('Heading, level 1', level=1)
    document.add_paragraph('Intense quote', style='IntenseQuote')

    document.add_paragraph(
        'first item in unordered list', style='ListBullet'
    )
    document.add_paragraph(
        'first item in ordered list', style='ListNumber'
    )

    document.add_page_break()

    document.save('demo.docx')

回答1:


Simple but hard to debug. Just add space between words in style.

document.add_paragraph('Intense quote', style='IntenseQuote')

Changes to:

document.add_paragraph('Intense quote', style='Intense Quote')



来源:https://stackoverflow.com/questions/28973277/python-docx-style-id-error-while-creating-a-word-document

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!