创建Word文档时出现python-docx style_id错误 [英] python-docx style_id error while creating a word document

查看:1380
本文介绍了创建Word文档时出现python-docx style_id错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在跟踪 python-docx 网站上的教程创建MS-Word文档,但出现错误:

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

推荐答案

简单但难以调试.只需在样式中的单词之间添加空格.

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

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

更改为:

document.add_paragraph('强引号',style ='强引号')

这篇关于创建Word文档时出现python-docx style_id错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆