···python
# 如何新建一个word文档
from docx import Document
document = Document()
# 如何打开一个现有的word文档,方式一:
document = Document('existing-document-file.docx')
document.save('new-file-name.docx')
# 如何打开一个现有的word文档,方式二:
f = open('foobar.docx', 'rb')
document = Document(f)
f.close()
# 如何打开一个现有的word文档,方式三:
with open('foobar.docx', 'rb') as f:
source_stream = StringIO(f.read())
document = Document(source_stream)
source_stream.close()
target_stream = StringIO()
document.save(target_stream)
···