When you run a mail merge, Word pulls names, addresses, and other information directly from your Excel spreadsheet into your labels or envelopes. The merge will run more smoothly if all the information you want to include is ready—so, the first step is to make sure your spreadsheet is formatted properly. Creating Your Excel Mailing List: Open a new Excel workbook and set up field names for your list in. I Don't Use “Send as Attachment” Excel has a built-in feature called Send as Attachment that will.
Introduction
I enjoy hearing from readers that have used concepts from this blog to solve their own problems.It always amazes me when I see examples where only a few lines of python code can solvea real business problem and save organizations a lot of time and money. I am also impressedwhen people figure out how to do this with no formal training - just with some hard work andwillingness to persevere through the learning curve.
This example comes from Mark Doll. I’ll turn it over to him to give his background:
I have been learning/using Python for about 3 years to help automate business processesand reporting. I’ve never had any formal training in Python, but found it to be a reliabletool that has helped me in my work.
Read on for more details on how Mark used Python to automate a very manual process of collecting andsorting Excel files to email to 100’s of users.
The Problem
Here’s Mark’s overview of the problem:
A business need arose to send out emails with Excel attachments to a list of~500 users and presented us with a large task to complete manually. Making this task harderwas the fact that we had to split data up by user from a master Excel file to create theirown specific file, then email that file out to the correct user.
Imagine the time it would take to manually filter, cut and paste the data into a file,then save it and email it out - 500 times! Using this Python approach we were able toautomate the entire process and save valuable time.
I have seen this type of problem multiple times in my experience. If you don’t have experiencewith a programming language, then it can seem daunting. With Python, it’s very feasible toautomate this tedious process. Here’s a graphical view of what Mark was able to do:
Solving the Problem
The first step is getting the imports in place:
Now we will set up some strings with the current date and our directory structure:
Let’s take a look at the data file we need to process:
The next step is to group all of the CUSTOMER_ID
transactions together. We start bydoing a groupby
on CUSTOMER_ID
.
It might not be apparent to you what customer_group
is in this case.A loop shows how we can process this grouped object:
Here’s the last group_df
that shows all of the transactions for customer A1005:
We have everything we need to create an Excel file for each customer and store in a directoryfor future use:
The attachments
list contains the customer ID and the full path to the file:
To make the processing easier, we convert the list to a DataFrame:
The final data prep stage is to generate a list of files with their email addresses bymerging the DataFrames together:
Which gives this simple DataFrame:
We’ve gathered the list of customers, their emails and the attachments. Now we need to sendan email with Outlook. Refer to this article for additional explanation of this code:
We can use this simple class to generate the emails and attach the Excel file.
The last step is to move the files to our archive directory:
Summary
This example does a nice job of automating a highly manual process where someonelikely did a lot of copying and pasting and manual file manipulation. I hope the solution thatMark developed can help you figure out how to automate some of the more painful parts ofyour job.
I encourage you to use this example to identify similar challenges in your day to day work.Maybe you don’t have to work with 100’s of files but you might have a manual process yourun once a week. Even if that process only takes 1 hour, use that as a jumping off point tofigure out how to use Python to make it easier. There is no better way to learn Pythonthan to apply it to one of your own problems.
Thanks again to Mark for taking the time to walk us through this content example!
Comments
Performing a Mail Merge is a great way to generate personalized letters or emails.
There are three documents involved in the mail merge process:
- Your main document
- Your data source
- Your merged document
NOTE: The examples shown below contain sample screen shots from Microsoft Word 2016. If you have a different version of Word, there may be some slight differences however, the same basic program flow should apply.
Step 1: Create your Excel Spreadsheet
The most important step in the mail merge process is to set up and prepare your data. You’ll use your Excel spreadsheet as the data source for your recipient list.
Here are some tips to prepare your data for a mail merge:
Excel Mail List
- Make sure the column names on your spreadsheet match the field names you want to insert in your mail merge
- All data to be merged is present in the first sheet of your spreadsheet
- All data entries with percentages, currencies, and zip codes are correctly formatted in the spreadsheet so that Word can properly read their values
- The Excel spreadsheet to be used in the mail merge is stored on your local computer
- Changes or additions to your spreadsheet are completed before it’s connected to your mail merge document in Word
Step 2: Create a mail merge document
When creating a mail merge you have the ability to use an existing document/letter.
In Word, open the existing file and press the ‘Mailings’ tab in the main menu.
On the Mailings tab, choose the ‘Start Mail Merge’ button, a list of different types of documents will drop down (i.e. Letters, E-mail Messages, etc.). choose the kind of merge you want to run.
Excel Mail Merge Labels
Now choose the ‘Select Recipients’ button and choose ‘Use an Existing List’
Browse to find your Excel spreadsheet you previously saved, and then choose ‘OK’.
NOTE: Now the Excel spreadsheet is connected to the mail merge document you’re creating in Word.
Step 3: Inserting the merge fields
Mail Merge From Excel To Word
You will notice menu items are now active that were not previously
You can insert one or more mail merge fields that pull the information from your spreadsheet into your document.
On your Word document, highlight the field you want to populate with the data from Excel.
On the Mailings tab, choose the ‘Insert Merge Field’ button, a list of the column headers on your saved excel document will drop down (i.e. Company Name, etc.). Choose the appropriate field you want to merge and choose Insert.
The highlighted field will be replaced with the merge field (i.e. «Company», etc.)
Mail Merge Excel To Excel
Repeat step 3 for each of the fields you want to merge and choose Close when done.
Now choose ‘Save’.
Step 4: Previewing the mail merge
After you insert the merge fields you want you can now preview the results to confirm that the document is set-up the way you want.
On the Mailings tab, choose ‘Preview Results’. The merge fields will be populated with the data on the first row of your excel document.
To move through the records in your data source and view how they will appear in the document choose the right arrow to advance or the left arrow for the previous record.
Step 5: Merging and Saving your mail merge
On the Mailings tab, choose the ‘Finish & Merge’ button, a list of different types of merges will drop down (i.e. Edit Individual Documents, Print Documents and Send Email Messages) choose ‘Edit Individual Documents’. A ‘Merge to New Document’ pop-up box will appear, choose ‘OK’.
You will now have a separate letter for each of the rows on the excel document which you can print, email, etc.
Step 6: Saving your mail merge document
When you are done working with your merged documents, save and close it as you would any other Word document. Once closed the mail merge template will still be open. If you plan to reuse it in the future, save it and it will stay connected to your data source.
NOTE: A mail merge can just as easily be an email merge. Add a column to your excel spreadsheet that contains the email address to send each merged letter to. Under 'Finish and Merge', select 'Send Email Messages' and for the TO specify the column name that contains the email address and specify the SUBJECT to use.