-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmerge_docx.py
More file actions
34 lines (22 loc) · 918 Bytes
/
Copy pathmerge_docx.py
File metadata and controls
34 lines (22 loc) · 918 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# -*- coding: utf-8 -*-
import sys
import argparse
from docx import Document
# TODO better?
# from https://stackoverflow.com/questions/24872527/combine-word-document-using-python-docx
def combine_word_documents(out_file, files):
merged_document = Document()
for index, file in enumerate(files):
sub_doc = Document(file)
# Don't add a page break if you've reached the last file.
if index < len(files)-1:
sub_doc.add_page_break()
for element in sub_doc.element.body:
merged_document.element.body.append(element)
merged_document.save(out_file)
parser = argparse.ArgumentParser(description='Merge few docx files into single file.')
parser.add_argument('OUTFILE', help='Output docx.')
parser.add_argument('INFILES', nargs='+', help='Input docx files')
args = parser.parse_args()
# print(args)
combine_word_documents(args.OUTFILE, args.INFILES)