Snhp9
Snhp9

Reputation: 529

How would I merge multiple documents together in pywin32?

I am working on a script that needs to grab all of the documents out of a directory and merge them together with comments and formatting.

I know VBS can do this but VBS is very limited and slow when it comes to parsing documents. Especially in word 2013.

I've looked over the documentation of pywin32 but couldn't find anything.

I would think it would be something as simple as

word = win32.Dispatch("Word.Application")
doc = word.AddDocument()
doc.InsertDocument(Filename)

There isn't much code to show because all the code I have currently is used after the documents are merged.

Upvotes: 1

Views: 1959

Answers (1)

NorthCat
NorthCat

Reputation: 9937

The code will look like this:

import win32com.client as win32

word = win32.gencache.EnsureDispatch('Word.Application')
word.Visible = False

output = word.Documents.Add()

output.Application.Selection.Range.InsertFile('second.doc')
output.Application.Selection.Range.InsertBreak()
output.Application.Selection.Range.InsertFile('first.doc')

output.SaveAs('output.doc')
output.Close()

This question also may be useful.

Upvotes: 2

Related Questions