Working with Documents

Opening a document

from docx import Document

document = Document()
document.save('test.docx')

REALLY opening a document

document = Document('existing-document-file.docx')
document.save('new-file-name.docx')

Opening a ‘file-like’ document

f = open('foobar.docx', 'rb')
document = Document(f)
f.close()

# or

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)
发布了7 篇原创文章 · 获赞 1 · 访问量 116

猜你喜欢

转载自blog.csdn.net/qq_25914817/article/details/104735318