I am running a mail merge from Excel to Word utilizing Python (Openpyxl). I'm running into a problem of blank values being merged in as a single space ' ' rather than just showing a true blank as they normally would. I have a numbered list that will pull 8 different merge fields (each to a new line) and should skip the number/line if the cell is blank. Is it possible to make openpyxl treat an empty cell as a true blank value rather than showing it as a blank space, which Word then merges in? A snippet of the mail merge code is below:
from __future__ import print_function
import os
import openpyxl
from mailmerge import MailMerge
from datetime import date
os. chdir(r'CURRENT WORKING FOLDER')
wb = openpyxl.load_workbook('FullMerge.xlsm', data_only=True)
sheet = wb["Database"]
max_col = 104
sheet.delete_rows(sheet.min_row, 1)
template = "FullMerge.docx"
document1 = MailMerge("FullMerge.docx")
First = str(sheet.cell(row = 1, column = 1).value or '')
Second = str(sheet.cell(row = 1, column = 2).value or '')
Third = str(sheet.cell(row = 1, column = 3).value or '')
document1.merge(
First = First,
Second = Second,
Third = Third
)
document1.write("FinishedMerge.docx")
EXAMPLE:
If the value in Second is blank and I manually mail merge, I get:
First Text
Third Text
If the value in Second is blank and I Python mail merge, I get:
First Text
'single blank space'
Third Text