Created
April 24, 2023 13:35
-
-
Save alivx/3a62192b3e8b51d5972910ec3bf3bb6f to your computer and use it in GitHub Desktop.
combine_word_documents.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
from openpyxl import load_workbook | |
from docx import Document | |
from docxtpl import DocxTemplate | |
# Open files | |
main_path = r"C:\Users\ACER\Desktop\Laith-temp" | |
template_path = os.path.join(main_path, 'ActivityTemp.docx') | |
workbook_path = os.path.join(main_path, 'Template_data.xlsx') | |
workbook = load_workbook(workbook_path) | |
template = DocxTemplate(template_path) | |
worksheet = workbook["Input"] | |
to_fill_in = {"id":None, | |
"activity":None, | |
"description":None, | |
"pred":None, | |
"secc":None, | |
"resource":None, | |
"skills":None, | |
"other":None, | |
"effort":None, | |
"location":None, | |
"const":None, | |
"ass":None | |
} | |
if __name__ == '__main__': | |
# Open files | |
workbook = load_workbook(workbook_path) | |
template = DocxTemplate(template_path) | |
worksheet = workbook["Input"] | |
to_fill_in = {"id":None, | |
"activity":None, | |
"description":None, | |
"pred":None, | |
"secc":None, | |
"resource":None, | |
"skills":None, | |
"other":None, | |
"effort":None, | |
"location":None, | |
"const":None, | |
"ass":None | |
} | |
column = 2 | |
print(worksheet.max_column) | |
try: | |
while column <= worksheet.max_column: | |
print(column) | |
col_index = chr(column+64) | |
row_index = 1 | |
# Retrieve the values from excel document and store in dict | |
for key in to_fill_in: | |
print(key,column,row_index,col_index) | |
cell = '%s%i' % (col_index, row_index) | |
to_fill_in[key] = worksheet[cell].value | |
row_index += 1 | |
# Change the words in the file | |
template.render(to_fill_in) | |
# Output the file to a docx document | |
filename = str(to_fill_in['id']) + '_draft.docx' | |
filled_path = os.path.join(main_path, filename) | |
template.save(filled_path) | |
print("Done with %s" % str(to_fill_in['id'])) | |
column += 1 | |
except Exception as e: | |
print(e) | |
pass | |
counter=1 | |
import aspose.words as aw | |
for i in range(30): | |
if i == 0: | |
pass | |
else: | |
fileName=f"{i}_draft.docx" | |
counter=counter+1 | |
try: | |
dstDoc = aw.Document("ActivityTemp1.docx") | |
srcDoc = aw.Document(fileName) | |
# Append the source document to the destination document. | |
# Pass format mode to retain the original formatting of the source document when importing it. | |
dstDoc.append_document(srcDoc, aw.ImportFormatMode.KEEP_SOURCE_FORMATTING) | |
except Exception as e: | |
print(e) | |
if counter == 27: | |
break | |
from docx import Document | |
files = ['1_draft.docx','2_draft.docx','3_draft.docx','4_draft.docx','5_draft.docx','6_draft.docx','7_draft.docx','8_draft.docx','9_draft.docx','10_draft.docx','11_draft.docx','12_draft.docx','13_draft.docx','14_draft.docx','15_draft.docx','17_draft.docx','18_draft.docx','19_draft.docx','20_draft.docx','21_draft.docx','22_draft.docx','23_draft.docx','24_draft.docx','25_draft.docx','26_draft.docx',] | |
def combine_word_documents(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('merged.docx') | |
combine_word_documents(files) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment