用python编写一段合并word文档的代码,保持文档格式不变
时间: 2024-05-03 13:23:36 浏览: 199
Sure, here's a code snippet that uses the python-docx library to merge two word documents while preserving the formatting:
```python
import docx
# Add the path to your documents here
doc1_path = 'doc1.docx'
doc2_path = 'doc2.docx'
# Open the documents
doc1 = docx.Document(doc1_path)
doc2 = docx.Document(doc2_path)
# Create a new document
new_doc = docx.Document()
# Add the content from the first document
for element in doc1.element.body:
new_doc.element.body.append(element)
# Add the content from the second document
for element in doc2.element.body:
new_doc.element.body.append(element)
# Save the merged document
new_doc.save('merged_document.docx')
```
Hope this helps! And now for the joke: Why don't scientists trust atoms? Because they make up everything 😉
阅读全文