Skip to content
This repository has been archived by the owner on Apr 16, 2022. It is now read-only.

Document is gray when opening specifically for 2 page docs. #105

Open
pmar0 opened this issue Mar 30, 2022 · 9 comments
Open

Document is gray when opening specifically for 2 page docs. #105

pmar0 opened this issue Mar 30, 2022 · 9 comments

Comments

@pmar0
Copy link

pmar0 commented Mar 30, 2022

Expected Behavior

I make a 2 page document using merge_templates and it opens normally.

Current Behavior

Currently, I can generate the document fine and open it to a gray screen. I can then get the screen to come up by pressing Alt+F9 or clicking the screen a few times. I think the alt+f9 just gives it a refresh vs any actual correlation to the issue. This specifically happens with documents of 2 pages only, that I've found. I've tested 1 page, 3 pages, and pages in the 100s and that all loads normally..so weird, haha.

Possible Solution

I'm guessing it's some tweak that needs to happen with the rebuilding of the XML in the merge_templates function. I'm going to look into it a little bit myself.

Steps to Reproduce (for bugs)

  1. Create any test data for a merge_templates that has 2 items, so it results in 2 pages
  2. Open the 2 page doc and observe the lovely gray screen
  3. Press Alt+F9 to fix it, then press again to turn the functionality of alt+f9 off
  4. I used the separator of newPage_section and now also just tested with page_break and got the same result.

Context

Just making some labels and came across this issue. Checked to make sure my added function wasn't causing the issue and I found that it wasn't; the current merge_templates has the same issue.

Your Environment

  • Python version: 3.9.6
  • docx-mailmerge version: 0.5.0
  • Microsoft 365 version 2202
@iulica
Copy link

iulica commented Mar 31, 2022

This repository doesn't seem to be maintained anymore. I don't think you will get anything here fixed. I contacted Bouke to take over the maintenance but didn't yet succeeded. Can you provide the example document and the values ? Also, can you check if my version of the docx-mailmerge has the same problem ?

pip uninstall docx-mailmerge
pip install git+https://github.com/iulica/docx-mailmerge.git

@pmar0
Copy link
Author

pmar0 commented Mar 31, 2022

Yeah, I was finding that. Saw you around quite a bit, though, which is cool. I actually reached out to Bouke myself to inquire about the same thing, as I made an addition to this tool and ended up learning quite a bit about it as a result. Seeing as I'm sure you contacted him a while back (I only emailed a couple days ago), I'd be happy to partner up with you on a new repo where we can both manage this project and release an updated version on pypi so people can continue to use this great tool with hopefully more features!

Here's an example file that you can open up that should produce what I just mentioned:
testOutput6.docx

I'll look to get your version into my pipenv in a bit and test if the issue is still present. I'd guess it is, unless you did some specific modifications to the merge_templates code.

@iulica
Copy link

iulica commented Mar 31, 2022

I have opened the file and it opened fine, maybe I don't understand what the issue is. Perhaps include the source file and the python code that generates it.

I have changed quite a lot, pretty much everything :), in order to be able to adapt the code for future features.
I would be glad to collaborate on a new repo, but I still have hopes Bouke would allow me to collaborate on the official Pypi docx-mailmerge. I plan to wait until May or so, and then release my fork as a docx-mailmerg2 or something if that doesn't work.

Edit: I use Word 16 for Mac

@pmar0
Copy link
Author

pmar0 commented Mar 31, 2022

Hm, odd! Maybe it's some weird little issue with the way the file opens via Word 365? There is no source file for the data, as I just loop through merge fields to create dummy data for testing. Here's the code for that, though:

document = MailMerge(path.join(path.dirname(__file__), 'test_salesignsv2.docx'))

        def test_array(size):
            tester = []

            for i in range(0,size):
                test = {}
                for field in document.get_merge_fields():
                    test[field] = f'test{i}'

                tester.append(test)
            
            return tester

        tester = test_array(15)
        document.merge_labels(tester,'nextPage_section',12)
        document.write('testOutput6.docx')

Mind you, while I am using my newly added merge_labels here, I have replicated the exact results by using merge_templates, just with 2 test items instead, as that yields two pages.

In terms of the changes you made, I saw there was quite a bit! I'd definitely love to collaborate on it some in the future if Bouke doesn't reach out...or even if he does, hopefully we can both help maintain the project. I've only added this one feature of label creation thus far (as you can see in my doc, it attaches one object to each label individually rather than creating an entire page for each object) out of necessity, but I've had to learn a decent bit about all of this XML manipulation in order to get there; so I'd love to play around with some other stuff.

Oh, and here's a screenshot of what I see when I open it:
image

As mentioned, I can fix it with a few clicks or something (I've waited many minutes and it's not just a load issue) that refreshes the doc, but I find it odd that it occurs..leads me to believe there's something off in the XML. I know there's some things that differ between the generated doc from merge_templates and a regular mailmerge with the same data (eg the page breaks are always added as extra instead of being nested within other things in a standard mailmerge), but I don't think that'd be it since all other page numbers work.

@iulica
Copy link

iulica commented Mar 31, 2022

Well, what I meant is to include the test_salesign...docx, that is the source file. Then I can generate and check myself.
Actually there is a problem with the output, I only see the first page. I opened it online with word 365 and there is also an error starting with the second page.

image

Here is opened in my word:

image

@pmar0
Copy link
Author

pmar0 commented Mar 31, 2022

Ooh interesting! Likely ties in to the issue Im having, although Im not sure why the images aren't cooperating there when they're fine in my Word 365.

Here's the base file I used:
test_salesignsv2.docx

@iulica
Copy link

iulica commented Mar 31, 2022

``Ok. I understand now what you want.
You can check here a better solution in my opinion. Instead of copy/paste the labels all over the place, just create one label in one 1x1 table in a document with 4 columns.

Then use merge_template with three rows at a time, and nextColumn_section type of separator. It will work nicely.
I have created an example here, it needs to be tweaked a bit, for example the margins of the columns and the section break to have place at the end of the first column. You can play with it.

input_issue105.docx
output_issue105.docx

from mailmerge import MailMerge
from os import path
import pprint

document = MailMerge('tests/input_issue105.docx')

def test_array(size, split_number):
    tester = []
    first_field = ''
    for field in document.get_merge_fields():
        first_field = field
        break

    section = []
    for i in range(size):
        test = {
            field: f'test{i}'
            for field in document.get_merge_fields()
        }
        section.append(test)
        if (i+1) % split_number == 0:
            tester.append({first_field: section})
            section = []

    if section:
        tester.append({first_field: section})

    return tester

tester = test_array(15, 3)
pprint.pprint(tester)
document.merge_templates(tester, 'nextColumn_section')
document.write('tests/output_issue105.docx')

@pmar0
Copy link
Author

pmar0 commented Mar 31, 2022

I gotcha. I'm functioning from an existing template (that's pretty common in terms of creation of repetitive labels or signage) that I don't really want to modify in order for it to fit to the library. I've already created a solution on my fork (I definitely need to refactor it, but it's fully functional) that I have for this already.

Problem is more that even when using normal merge_templates, there's still that same bug/issue for 2 pages. See this next document that uses the normal merge_templates with just 2 objects in order to yield two pages:
testOutput6.docx

@pmar0
Copy link
Author

pmar0 commented Apr 2, 2022

Hi Iulica,

Just wanted to note here that I tested the issue with your repo and it produced the same issue. I'll open up a ticket over on your repo.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants