I am trying to mail merge an excel file into a docx file. My excel file looks something like this:
student_number Student Barcode Grade Pin School
43253 Jon *4563* 2 4563 Midland
31242 Ron *4321* 3 4321 Midland
The columns that I want to merge to the word template are Student, and Barcode. I have mail merge fields in the word template already, I am just trying to connect the columns in the excel document to the word template. I am new to programming and any help would be greatly appreciated. Here is my code so far:
from openpyxl import load_workbook
from mailmerge import MailMerge
# Setting up Excel sheet variables
wb = load_workbook('C:\\Users\\ansmith\\Documents\\Lunch-
Badges\\Badges.xlsx') #open excel workbook
sheet = wb['Badges'] #Tab to get information
max_row = sheet.max_row #count of all of the rows
# Getting Unique students Need to make each of their reports
stu_list = []
for cell_row in range(2 , max_row+1):
stu = sheet.cell(row = cell_row, column = 2).value
stu_list.append(stu)
unique_stu_list = list(set(stu_list)) #getting unique list of
students
Barcode_list = []
for cell_row in range(2 , max_row+1):
bar = sheet.cell(row = cell_row, column = 3).value
Barcode_list.append(bar)
unique_bar_list = list(set(Barcode_list))
# For each stu, create their order reports
for stu in unique_stu_list:
sales_history_list = [] #needed to create the docs dynamically
# Setting up Word document variables
template_doc = "C:\\Users\\ansmith\\Python_Badges\\FS Labels
Final.docx"
word_doc = MailMerge(template_doc)
# Merging the name and formatting totals
word_doc.merge(Student = stu)
word_doc.merge(Barcode = bar)
word_doc.write(f'{stu}.docx')
My code will Fill in the Student Name, but it doesn't fill the Barcode.