0

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

Phil
  • 151
  • 1
  • 8

1 Answers1

0

Edited to take account of the revised question.

Here, a value of '' (empty string) merges as an empty string, as I would expect, not as a ' ' (i.e. a space). So I assume that the problem is really that docx-mailmerge does not do the same suppression of empty lines that Word does. I don't think it actually has anything to do with openpyxl.

The code for docx-mailmerge is quite small - it's a few hundred lines of python - and it only really does substitution of { MERGEFIELD } fields by the values you provide. It doesn't really deal with any other merge field constructs such as IF fields or field switches. If it processed IF fields then you would could deal with line suppression using that mechanism. But I think it would need quite a substantial change to the docx-mailmerge code to do it. (the code would probably have to "remember" where it had done every substitution, then concatenate all the <w:t/> elements within a <w:p/> (Paragraph) element and remove the <w:p/> if (a) there was no text or other object in there) and (b) removing the <w:p/> element did not result in an invalid .docx.

Otherwise, it's a question of whether or not there is another library that does what you need. (Off-topic question here, unfortunately!)

Original text of the Answer:

"None" is the value of the Python "NoneType" variable, and tells us that openpyxl is interpreting an empty cell as "NoneType". Actually, I am sure there are a lot of things wrong with what I just said from a Python point of view, but

a. it's actually a good thing that openpyxl returns "None" in this scenario because it allows you to decide what you really want to insert in your merge. For some field types, for example, you might want to insert "0"

b. There is a discussion about how to deal with None here . In the specific example you give, you could use this

myval = str(sheet.cell(row = 1, column = 1).value or '')

but IMO some people would be happier to use a function that made it more obvious what was going on.

  • Thanks for the help (I'm still learning SF etiquette ie posting an updated issue vs editing a post). I've updated my post accordingly. – Phil Jun 29 '20 at 21:01
  • @Phil - looks like the Str(s or '') syntax didn't work for you - how about the other approaches people made in the other posts in the link about "None" ? –  Jun 29 '20 at 22:05
  • Adding (or '') did indeed result in a blank string. The problem is that when it merges, Word is picking the blank string up as a single blank space instead of a true no character string. So instead of skipping the merge field due to it being empty, Word is merging in a single blank space. – Phil Jun 30 '20 at 02:03
  • @Phil - I've modified my Answer. But it's not good news, sorry! –  Jun 30 '20 at 12:06