1

I have been trying to write a python script to mail merge labels. It would need to allow me to look into a folder, open an excel document, merge the document, and print it as a pdf. All the rows in each excel file are part of the same document and I'd like for them to be printed together. I've written up a script that opens a word template and pulls up the excel file to populate into the mail merge but when I print it:

  1. The printed copy only shows me the merge fields not the information on the workbook
  2. Only prints the first page, some of the files I use to make labels would make more than one page.

I've included the code that I have as well as pictures of what I'm currently getting and what I need the end Product to look like.

If anyone can help me on this, you would be a live saver.

What I need: What I need

What I'm getting:

What I'm getting

from os import listdir
import win32com.client as win32
import pathlib
import os
import pandas as pd
pd.options.mode.chained_assignment = None

working_directory = os.getcwd()
path = pathlib.Path().resolve()
inputPath = str(path) + '\Output'
outputPath = str(path) + '\OutputPDF'
inputs = listdir(inputPath)
wordApp = win32.Dispatch('Word.Application')
wordApp.Visible = True

sourceDoc = wordApp.Documents.Open(os.path.join(working_directory, 'labelTemplate.docx'))
mail_merge = sourceDoc.MailMerge


for x in inputs[1:]:
    mail_merge.OpenDataSource(inputPath + '/'+ x)
    print (x)  

    y = x.replace('.xlsx', '')
    z = y.replace('output_','')
    print (z)
    mail_merge = wordApp.ActiveDocument
    mail_merge.ExportAsFixedFormat(os.path.join(outputPath, z), exportformat:=17)`

0 Answers0