问题
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:
- Open each .docx file
- Append one line to the document; "This is checked"
- 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