-2

I hope to use read and write method to merge two word docs into another, but it only can write the content of the f1 word document successfully. Writing the f2 word doc does not work. I tried the following:

# coding=utf-8
f=open('C:\Users\Desktop\word.doc','ab')
f1=open('C:\Users\Desktop\word1.doc','rb')
f2=open('C:\Users\Desktop\word2.doc','rb')
data1=f1.read()
data2=f2.read()
f.write(data1)
f.write(data2)
f1.close()
f2.close()
f.close()
m00am
  • 5,910
  • 11
  • 53
  • 69
  • 1
    Please provide detailed information about the error you encountered according to [the following guide](https://stackoverflow.com/help/mcve). You may also want to get acquainted with [How to ask?](https://stackoverflow.com/help/how-to-ask). – sophros Dec 10 '18 at 07:18

1 Answers1

1

Microsoft Word document format is much more than pure text. Simply concatenating two documents will not work at all and this is what you are effectively doing.

The proper way to concatenate two documents in DOCX format would be to open them using an appropriate module - e.g. python-docx (or docx) - that understands the internal structure of the document (which is a zip-compressed folder with numerous XML files - you can check it yourself changing the extension and decompressing the contents).

The recipe how to concatenate two Word documents should prove helpful.

sophros
  • 14,672
  • 11
  • 46
  • 75
  • thanks for your answer, it seems could combine tow docs,but could not copy pics and tables. please see below answer i posted in "enter image description here" show the problem. – tangfengsongyu Dec 10 '18 at 08:28