Read and Write .docx file with python

江枫思渺然 提交于 2019-12-09 07:51:41

问题


I have a folder containing several .docx files with names [Code2001.docx, Code2002.docx... Code2154.docx].

I'm trying to write a script that will:

  1. Open each .docx file
  2. Append one line to the document; "This is checked"
  3. Save the .docx-file to another folder, named "Code2001_checked"

After searching I've only managed to get the filename with the loop:

import os
os.chdir(r"E:......\test")

for files in os.listdir("."):
    if files.endswith(".docx"):
        print filename

I also found this: docx module but the documentation is poor to continue.

Any suggestions on how to finish this script?


回答1:


from docx import *
document = opendocx("document.doc")
body = document.xpath('/w:document/w:body', namespaces=nsprefixes)[0]
body.append(paragraph('Appending this.'))

The second line may need to chance depending on where in the file you are going to append the text. To finish this, you will need to use the savedocx() function, and there is an example of its usage in the root of the project.



来源:https://stackoverflow.com/questions/17615404/read-and-write-docx-file-with-python

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