Indranil012
Indranil012

Reputation: 1

Need help to make mail merge fully automated with python and without MS office installed

I am doing mail merge using docxtpl but to make it fully automated I have to make the context dynamic so I don't have to make change in code everytime for the different templates.

Here is the code:

import pandas as pd
import os
from docxtpl import DocxTemplate

#Enter the complete filepath to the Word Template
#template_path = input("Insert the template file path : ")
template_path = os.getcwd() + "/sampledoc.docx"
print("Reading template from: " + template_path)

#For print variables
tpl = DocxTemplate(template_path)
variables = tpl.get_undeclared_template_variables()
print(f"\n Reading variables\n{variables}")

#Enter the complete filepath to the excel file which has the data
#source_path = input("Insert the source data file path : ")
source_path = os.getcwd() + "/source_data.xlsx"
print("\nReading source data from: " + source_path)
df = pd.read_excel(source_path)
print(f"\nPrinting source data\n{df}")

#Enter the complete filepath to the excel file where exported files will be saved
#save_dir = input("InserInsert the export directory path : ")
#os.chdir("export")
save_dir = "/storage/emulated/0/python/export/"

for variable in variables:
  locals()[variable] = df[variable].values
  
zipped = zip(Name,Age,Roll,Class,Score)
print("\nPerforming Mail Merge !")

for a,b,c,d,e in zipped:
  context = {"Name":a,"Age":b,"Roll":c,"Class":d,"Score":e}
  tpl.render(context)
  tpl.save(save_dir + '{}.docx'.format(a))
    
print("\nCongratulation! All files are exported to:\n"
       + str(save_dir[:-1]))

Want to change these bellow line dynamic so I don't have write the field name for different templates.

zipped = zip(Name,Age,Roll,Class,Score

context = {"Name":a,"Age":b,"Roll":c,"Class":d,"Score":e}

Upvotes: -3

Views: 1010

Answers (1)

Indranil012
Indranil012

Reputation: 1

If anyone need the code by help of @furas

The code is:

for index, row in df[variables].iterrows():
    context = row.to_dict()
    tpl.render(context)
    tpl.save(save_dir + '{}.docx'.format(context["Name"]))  

The full code is also given bellow:

import pandas as pd
import os
from docxtpl import DocxTemplate

#Enter the complete filepath to the Word Template
#template_path = input("Insert the Word template file path : ")
template_path = os.getcwd() + "/sampledoc.docx"
print("\nReading template from: " + template_path)

#To print variables
tpl = DocxTemplate(template_path)
variables = list(tpl.get_undeclared_template_variables())
print(f"\nReading variables\n{variables}")

#Enter the complete filepath of the excel file which has the data
#source_path = input("Insert the source Excel data file path : ")
source_path = os.getcwd() + "/source_data.xlsx"
print("\nReading source data from: " + source_path)
df = pd.read_excel(source_path)
print(f"\nPrinting source data\n{df}")

#Enter the complete directory path where exported files to be saved
#save_dir = input("Insert the export directory path : ")
#save_dir = "/storage/emulated/0/python/export/"
os.chdir("export")
save_dir = os.getcwd() + "/"

for index, row in df[variables].iterrows():
    context = row.to_dict()
    tpl.render(context)
    tpl.save(save_dir + '{}.docx'.format(context["Name"]))
    
print("\nCongratulation! All files are exported to:\n" + save_dir)

Upvotes: 0

Related Questions