iulica / docx-mailmerge

Mail merge for Office Open XML (docx) files without the need for Microsoft Office Word.
MIT License
60 stars 8 forks source link

List out of range error when opening file #25

Closed dwasyl closed 3 weeks ago

dwasyl commented 1 month ago

Hi there,

I have a particular docx file that I'm trying to use for a merge, but I keep getting a list index out of range error related to _get_field_type (specifically line 393 return s[0], s[1:] where instr is an empty string. Presumably an empty string shouldn't even be able to get to this point, so there's probably some error checking that's missed along the way.

I'm running into this when I run doc = MailMerge(file, remove_empty_tables=False, auto_update_fields_on_open="no") so that I can run doc.get_merge_fields().

I've tried going through the code base a bit to try and figure out the error, but clearly there is something about the way it's processing the source docx file that's causing this and just wondering if there were any initial ideas as to what might cause this problem. As it stands, I can't figure out it out enough to check for the issue in advance.

For reference, it's running Python 3.9.6, and docx-mailmerge2 version 0.6.3.

dwasyl commented 1 month ago

After some additional testing, the error seems to occur when the merge fields are located in a table cell. When the fields are moved outside of the table, there is no problem at all.

Any idea why this would cause a problem?

iulica commented 4 weeks ago

I'll have a look into it, can you provide a sample docx so that I can reproduce the error ?

dwasyl commented 3 weeks ago

@iulica Sure, here you go. Doc merge table simple sample.docx

iulica commented 3 weeks ago

Oh, I now noticed you are using the 0.6.3 version, that's quite old. Would you care testing it with the latest version ? Your document seems to work fine for me.

iulica commented 3 weeks ago

Ok, I could reproduce it myself.

The problem was an empty field (I created it by pressing alt-f9) or something like this. It doesn't matter where it is, in a table or not. Your document didn't have it so it worked fine.

For the moment, you'll have to find this empty field and delete it from the document. You can see the fields by pressing ctrl-f9 or something like this, I have a mac and it works by pressing option-f9.

image

I will fix this in the next release, but for the moment it's not critical.

iulica commented 3 weeks ago

I included a fix and released it with 0.8.2

dwasyl commented 3 weeks ago

Great find! I hadn't even noticed that, but makes sense why it would cause that error.

Appreciate the fix so quickly!