Python 3 - 如何使用 python docx 库删除 word 文档中的字母
Python 3 - How to remove letters on a word document with python docx library
alphabet_dic = ['a','b','c','d','e','f','g','h','i','j','k','l','n','o','p','q','r','s','t','u','v','w','y','z','m']
doc = Document('template.docx')
firstSen = doc.paragraphs[0].text
rep_dic = {ord(k):None for k in c_dic + [x.upper() for x in c_dic]}
translation = (firstSen.translate(rep_dic))
doc.save('translation.docx')
我正在尝试擦除文档中的一系列字符,但我所做的不起作用。我用常规字符串测试了我的代码,它运行良好,但是一旦我将它应用到文档中,它就完全没有变化。没有字母被删除。
有更好的解决方案或方向的建议吗?
正如 Jean-François Fabre 所说,您不会更改文档,因此您需要在保存之前将行写入文档,如下所示:
doc.paragraphs[0].text = translation
doc.save('translation.docx')
alphabet_dic = ['a','b','c','d','e','f','g','h','i','j','k','l','n','o','p','q','r','s','t','u','v','w','y','z','m']
doc = Document('template.docx')
firstSen = doc.paragraphs[0].text
rep_dic = {ord(k):None for k in c_dic + [x.upper() for x in c_dic]}
translation = (firstSen.translate(rep_dic))
doc.save('translation.docx')
我正在尝试擦除文档中的一系列字符,但我所做的不起作用。我用常规字符串测试了我的代码,它运行良好,但是一旦我将它应用到文档中,它就完全没有变化。没有字母被删除。
有更好的解决方案或方向的建议吗?
正如 Jean-François Fabre 所说,您不会更改文档,因此您需要在保存之前将行写入文档,如下所示:
doc.paragraphs[0].text = translation
doc.save('translation.docx')