UserWarning: style lookup by style

    科技2022-07-10  107

    Error:

    在学习python《AUTOMATE THE BORING STUFF WITH PYTHON》一书Chapter13有关word的处理,在程序运行中碰到错误:UserWarning: style lookup by style_id is deprecated. Use style name as key instead. return self._get_style_id_from_style(self[style_name], style_type)

    部分截图

    Solution:

    划线部分较为啰嗦,可以学习下思想,也可以直接跳过看代码:

    我在解决问题的时候浏览了一篇网页:https://www.pythonheidong.com/blog/article/167683/,这个上面提供的解决方案我本人看着没有什么技术性,所以没有仔细阅读,我本人发现作者提供了两个网站,点击https://python-docx.readthedocs.io/en/latest/user/styles-understanding.html进去浏览了一下,有了一个想法是不是代码中的参数有错误:如图:

     Keywords:或许就是这么巧合吧,把代码中的参数改成 'Quote Char'就把问题解决了

    附上修正后的代码:

    doc = docx.Document('demo.docx') print(doc.paragraphs[0].text) print(doc.paragraphs[0].style) doc.paragraphs[0].style = 'Normal' print(doc.paragraphs[1].text) print(doc.paragraphs[1].runs[0].text, doc.paragraphs[1].runs[1].text, doc.paragraphs[1].runs[2].text, doc.paragraphs[1].runs[3].text, doc.paragraphs[1].runs[4].text) doc.paragraphs[1].runs[0].style = 'Quote Char' doc.paragraphs[1].runs[1].underline = True doc.paragraphs[1].runs[3].underline = True doc.save('restyled.docx')

    注:我们渴望获得知识,在提升技术的路上不断学习,如果你在我这里得到了帮助,问题被解决了.希望你也能和我一样将自己遇到的问题及解决方案整理出来,供大家学习交流。我是一名Python菜鸟级玩家,如果有兴趣,可以互相交流学习,一起打怪升级。QQ:1755826272

    Processed: 0.012, SQL: 8