Python: How to iterate through a DataFrame into MailMerge fields?












0














The problem is that each file that is produced using the MailMerge method all have the same merge field data from the first iteration through the i loop, even though the same index conventions called printed through the loop i[0] will display properly as I print each iteration as well as those same index references are used to name each .docx as expected each time through the loop.



Code:



import pandas as pd
from mailmerge import MailMerge
import numpy as np

df1 = xl.parse('Sheet1')
df2 = pd.DataFrame(df1)
data1 = np.array(df2)
template = "Form_Template.docx"
document = MailMerge(template)



for i in data1:
document.merge(
Name= i[0],
Domain= 'fruitcorp.local',
userid= i[1],
Password= '*',
date_ini='{:%d-%b-%Y}_______'.format(date.today()),
comment='* This is a replacement for a missing original form.
Original start date was: ',
startdate= str(i[3]))
print(i[0])
document.write(i[1] + 'Replacement_Account_Acceptance_Form.docx')


Here is the displayed output that I get:



 In: print(data1):
Out:[['Apple Jacks' 'ajacks' Timestamp('2015-07-26 18:49:11') '26-Jul-015']
['Orange Gladys' 'ogladys' Timestamp('2015-01-05 18:50:38')
'05-Jan-2015']
['Ed Banana' 'ebanana' Timestamp('2017-01-09 18:51:47') '09-Jan-2017']
['Kiwi Lime' 'klime' Timestamp('2015-02-09 18:52:42') '09-Feb-2015']
['Pie Cheesecake' 'pcheesecake' Timestamp('2011-07-28 14:06:26')
'28-Jul-2011']]

#iterated properly:

In: for i in data1:
print(i[0])
Out: Apple Jacks
Orange Gladys
Ed Banana
Kiwi Lime
Pie Cheesecake


But the same index call to




i[0]




in the 'Name' field of document.merge function and i[0] similarly for the 'userid' field, results in the iterating value to remain as ['Apple Jacks' 'ajacks' Timestamp('2015-07-26 18:49:11') '26-Jul-2015'] each iteration



In: for i in data1:
document.merge(
Name= i[0],
startdate= str(i[3]),
userid= i[1]))

Out: Name= Apple Jacks
startdate= 26-Jul-2015
userid= ajacks

Name= Apple Jacks
startdate= 26-Jul-2015
userid= ajacks

Name= Apple Jacks
startdate= 26-Jul-2015
userid= ajacks

Name= Apple Jacks
startdate= 26-Jul-2015
userid= ajacks

Name= Apple Jacks
startdate= 26-Jul-2015
userid= ajacks


The same data is repeated each (5) times rather than producing 5 unique sets
All I can think of is that I am not using the DataFrame index properly when I am within the MailMerge method.




... Use i[0][0] instead?




In: for i in data1:
document.merge(
Name= i[0][0],
startdate= str(i[3][0]),
userid= i[1][0]))

Out: Name= A
startdate= 2
userid= a

Name= A
startdate= 2
userid= a

Name= A
startdate= 2
userid= a

Name= A
startdate= 2
userid= a

Name= A
startdate= 2
userid= a


(Would this be far simpler to access my relational data as a dictionary?)
The main question is: How can I best get each produced document to contain the correct field data from each iteration of the loop rather than it currently each contains the first row data (all produced documents contain: 'Apple Jacks, ajacks, 26-Jul-2015' in their merge fields)










share|improve this question
























  • This does not feel like it's a minimal example. See Minimal, Complete, and Verifiable example.
    – timgeb
    Dec 27 '18 at 21:52










  • My apologies for not making this a minimal example. I am new here and also to python, and I thought it would be helpful to show the full methodology in the case where I have gone about this completely wrong
    – DavidK - Computer Scientist
    Dec 27 '18 at 22:19










  • If I understand correctly, you simply need to index one more time: if i[0] is ['Apple Jacks', 'ajacks', Timestamp('2015-07-26 18:49:11'), '26-Jul-2015'], then i[0][0] is 'Apple Jacks'.
    – mkrieger1
    Dec 28 '18 at 0:15










  • Although I think I don't understand correctly, there is so much text. Can you simply edit your question to read like this: (1) this is my code, (2) this is my input, (3) this is what I get, (4) this is what I would like to get.
    – mkrieger1
    Dec 28 '18 at 0:17










  • And by "this is what I get" I mean exactly what you get copy/pasted, not described in your own words.
    – mkrieger1
    Dec 28 '18 at 0:18
















0














The problem is that each file that is produced using the MailMerge method all have the same merge field data from the first iteration through the i loop, even though the same index conventions called printed through the loop i[0] will display properly as I print each iteration as well as those same index references are used to name each .docx as expected each time through the loop.



Code:



import pandas as pd
from mailmerge import MailMerge
import numpy as np

df1 = xl.parse('Sheet1')
df2 = pd.DataFrame(df1)
data1 = np.array(df2)
template = "Form_Template.docx"
document = MailMerge(template)



for i in data1:
document.merge(
Name= i[0],
Domain= 'fruitcorp.local',
userid= i[1],
Password= '*',
date_ini='{:%d-%b-%Y}_______'.format(date.today()),
comment='* This is a replacement for a missing original form.
Original start date was: ',
startdate= str(i[3]))
print(i[0])
document.write(i[1] + 'Replacement_Account_Acceptance_Form.docx')


Here is the displayed output that I get:



 In: print(data1):
Out:[['Apple Jacks' 'ajacks' Timestamp('2015-07-26 18:49:11') '26-Jul-015']
['Orange Gladys' 'ogladys' Timestamp('2015-01-05 18:50:38')
'05-Jan-2015']
['Ed Banana' 'ebanana' Timestamp('2017-01-09 18:51:47') '09-Jan-2017']
['Kiwi Lime' 'klime' Timestamp('2015-02-09 18:52:42') '09-Feb-2015']
['Pie Cheesecake' 'pcheesecake' Timestamp('2011-07-28 14:06:26')
'28-Jul-2011']]

#iterated properly:

In: for i in data1:
print(i[0])
Out: Apple Jacks
Orange Gladys
Ed Banana
Kiwi Lime
Pie Cheesecake


But the same index call to




i[0]




in the 'Name' field of document.merge function and i[0] similarly for the 'userid' field, results in the iterating value to remain as ['Apple Jacks' 'ajacks' Timestamp('2015-07-26 18:49:11') '26-Jul-2015'] each iteration



In: for i in data1:
document.merge(
Name= i[0],
startdate= str(i[3]),
userid= i[1]))

Out: Name= Apple Jacks
startdate= 26-Jul-2015
userid= ajacks

Name= Apple Jacks
startdate= 26-Jul-2015
userid= ajacks

Name= Apple Jacks
startdate= 26-Jul-2015
userid= ajacks

Name= Apple Jacks
startdate= 26-Jul-2015
userid= ajacks

Name= Apple Jacks
startdate= 26-Jul-2015
userid= ajacks


The same data is repeated each (5) times rather than producing 5 unique sets
All I can think of is that I am not using the DataFrame index properly when I am within the MailMerge method.




... Use i[0][0] instead?




In: for i in data1:
document.merge(
Name= i[0][0],
startdate= str(i[3][0]),
userid= i[1][0]))

Out: Name= A
startdate= 2
userid= a

Name= A
startdate= 2
userid= a

Name= A
startdate= 2
userid= a

Name= A
startdate= 2
userid= a

Name= A
startdate= 2
userid= a


(Would this be far simpler to access my relational data as a dictionary?)
The main question is: How can I best get each produced document to contain the correct field data from each iteration of the loop rather than it currently each contains the first row data (all produced documents contain: 'Apple Jacks, ajacks, 26-Jul-2015' in their merge fields)










share|improve this question
























  • This does not feel like it's a minimal example. See Minimal, Complete, and Verifiable example.
    – timgeb
    Dec 27 '18 at 21:52










  • My apologies for not making this a minimal example. I am new here and also to python, and I thought it would be helpful to show the full methodology in the case where I have gone about this completely wrong
    – DavidK - Computer Scientist
    Dec 27 '18 at 22:19










  • If I understand correctly, you simply need to index one more time: if i[0] is ['Apple Jacks', 'ajacks', Timestamp('2015-07-26 18:49:11'), '26-Jul-2015'], then i[0][0] is 'Apple Jacks'.
    – mkrieger1
    Dec 28 '18 at 0:15










  • Although I think I don't understand correctly, there is so much text. Can you simply edit your question to read like this: (1) this is my code, (2) this is my input, (3) this is what I get, (4) this is what I would like to get.
    – mkrieger1
    Dec 28 '18 at 0:17










  • And by "this is what I get" I mean exactly what you get copy/pasted, not described in your own words.
    – mkrieger1
    Dec 28 '18 at 0:18














0












0








0







The problem is that each file that is produced using the MailMerge method all have the same merge field data from the first iteration through the i loop, even though the same index conventions called printed through the loop i[0] will display properly as I print each iteration as well as those same index references are used to name each .docx as expected each time through the loop.



Code:



import pandas as pd
from mailmerge import MailMerge
import numpy as np

df1 = xl.parse('Sheet1')
df2 = pd.DataFrame(df1)
data1 = np.array(df2)
template = "Form_Template.docx"
document = MailMerge(template)



for i in data1:
document.merge(
Name= i[0],
Domain= 'fruitcorp.local',
userid= i[1],
Password= '*',
date_ini='{:%d-%b-%Y}_______'.format(date.today()),
comment='* This is a replacement for a missing original form.
Original start date was: ',
startdate= str(i[3]))
print(i[0])
document.write(i[1] + 'Replacement_Account_Acceptance_Form.docx')


Here is the displayed output that I get:



 In: print(data1):
Out:[['Apple Jacks' 'ajacks' Timestamp('2015-07-26 18:49:11') '26-Jul-015']
['Orange Gladys' 'ogladys' Timestamp('2015-01-05 18:50:38')
'05-Jan-2015']
['Ed Banana' 'ebanana' Timestamp('2017-01-09 18:51:47') '09-Jan-2017']
['Kiwi Lime' 'klime' Timestamp('2015-02-09 18:52:42') '09-Feb-2015']
['Pie Cheesecake' 'pcheesecake' Timestamp('2011-07-28 14:06:26')
'28-Jul-2011']]

#iterated properly:

In: for i in data1:
print(i[0])
Out: Apple Jacks
Orange Gladys
Ed Banana
Kiwi Lime
Pie Cheesecake


But the same index call to




i[0]




in the 'Name' field of document.merge function and i[0] similarly for the 'userid' field, results in the iterating value to remain as ['Apple Jacks' 'ajacks' Timestamp('2015-07-26 18:49:11') '26-Jul-2015'] each iteration



In: for i in data1:
document.merge(
Name= i[0],
startdate= str(i[3]),
userid= i[1]))

Out: Name= Apple Jacks
startdate= 26-Jul-2015
userid= ajacks

Name= Apple Jacks
startdate= 26-Jul-2015
userid= ajacks

Name= Apple Jacks
startdate= 26-Jul-2015
userid= ajacks

Name= Apple Jacks
startdate= 26-Jul-2015
userid= ajacks

Name= Apple Jacks
startdate= 26-Jul-2015
userid= ajacks


The same data is repeated each (5) times rather than producing 5 unique sets
All I can think of is that I am not using the DataFrame index properly when I am within the MailMerge method.




... Use i[0][0] instead?




In: for i in data1:
document.merge(
Name= i[0][0],
startdate= str(i[3][0]),
userid= i[1][0]))

Out: Name= A
startdate= 2
userid= a

Name= A
startdate= 2
userid= a

Name= A
startdate= 2
userid= a

Name= A
startdate= 2
userid= a

Name= A
startdate= 2
userid= a


(Would this be far simpler to access my relational data as a dictionary?)
The main question is: How can I best get each produced document to contain the correct field data from each iteration of the loop rather than it currently each contains the first row data (all produced documents contain: 'Apple Jacks, ajacks, 26-Jul-2015' in their merge fields)










share|improve this question















The problem is that each file that is produced using the MailMerge method all have the same merge field data from the first iteration through the i loop, even though the same index conventions called printed through the loop i[0] will display properly as I print each iteration as well as those same index references are used to name each .docx as expected each time through the loop.



Code:



import pandas as pd
from mailmerge import MailMerge
import numpy as np

df1 = xl.parse('Sheet1')
df2 = pd.DataFrame(df1)
data1 = np.array(df2)
template = "Form_Template.docx"
document = MailMerge(template)



for i in data1:
document.merge(
Name= i[0],
Domain= 'fruitcorp.local',
userid= i[1],
Password= '*',
date_ini='{:%d-%b-%Y}_______'.format(date.today()),
comment='* This is a replacement for a missing original form.
Original start date was: ',
startdate= str(i[3]))
print(i[0])
document.write(i[1] + 'Replacement_Account_Acceptance_Form.docx')


Here is the displayed output that I get:



 In: print(data1):
Out:[['Apple Jacks' 'ajacks' Timestamp('2015-07-26 18:49:11') '26-Jul-015']
['Orange Gladys' 'ogladys' Timestamp('2015-01-05 18:50:38')
'05-Jan-2015']
['Ed Banana' 'ebanana' Timestamp('2017-01-09 18:51:47') '09-Jan-2017']
['Kiwi Lime' 'klime' Timestamp('2015-02-09 18:52:42') '09-Feb-2015']
['Pie Cheesecake' 'pcheesecake' Timestamp('2011-07-28 14:06:26')
'28-Jul-2011']]

#iterated properly:

In: for i in data1:
print(i[0])
Out: Apple Jacks
Orange Gladys
Ed Banana
Kiwi Lime
Pie Cheesecake


But the same index call to




i[0]




in the 'Name' field of document.merge function and i[0] similarly for the 'userid' field, results in the iterating value to remain as ['Apple Jacks' 'ajacks' Timestamp('2015-07-26 18:49:11') '26-Jul-2015'] each iteration



In: for i in data1:
document.merge(
Name= i[0],
startdate= str(i[3]),
userid= i[1]))

Out: Name= Apple Jacks
startdate= 26-Jul-2015
userid= ajacks

Name= Apple Jacks
startdate= 26-Jul-2015
userid= ajacks

Name= Apple Jacks
startdate= 26-Jul-2015
userid= ajacks

Name= Apple Jacks
startdate= 26-Jul-2015
userid= ajacks

Name= Apple Jacks
startdate= 26-Jul-2015
userid= ajacks


The same data is repeated each (5) times rather than producing 5 unique sets
All I can think of is that I am not using the DataFrame index properly when I am within the MailMerge method.




... Use i[0][0] instead?




In: for i in data1:
document.merge(
Name= i[0][0],
startdate= str(i[3][0]),
userid= i[1][0]))

Out: Name= A
startdate= 2
userid= a

Name= A
startdate= 2
userid= a

Name= A
startdate= 2
userid= a

Name= A
startdate= 2
userid= a

Name= A
startdate= 2
userid= a


(Would this be far simpler to access my relational data as a dictionary?)
The main question is: How can I best get each produced document to contain the correct field data from each iteration of the loop rather than it currently each contains the first row data (all produced documents contain: 'Apple Jacks, ajacks, 26-Jul-2015' in their merge fields)







python-3.x dataframe for-loop mailmerge numpy-ndarray






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 28 '18 at 21:52

























asked Dec 27 '18 at 21:48









DavidK - Computer Scientist

43




43












  • This does not feel like it's a minimal example. See Minimal, Complete, and Verifiable example.
    – timgeb
    Dec 27 '18 at 21:52










  • My apologies for not making this a minimal example. I am new here and also to python, and I thought it would be helpful to show the full methodology in the case where I have gone about this completely wrong
    – DavidK - Computer Scientist
    Dec 27 '18 at 22:19










  • If I understand correctly, you simply need to index one more time: if i[0] is ['Apple Jacks', 'ajacks', Timestamp('2015-07-26 18:49:11'), '26-Jul-2015'], then i[0][0] is 'Apple Jacks'.
    – mkrieger1
    Dec 28 '18 at 0:15










  • Although I think I don't understand correctly, there is so much text. Can you simply edit your question to read like this: (1) this is my code, (2) this is my input, (3) this is what I get, (4) this is what I would like to get.
    – mkrieger1
    Dec 28 '18 at 0:17










  • And by "this is what I get" I mean exactly what you get copy/pasted, not described in your own words.
    – mkrieger1
    Dec 28 '18 at 0:18


















  • This does not feel like it's a minimal example. See Minimal, Complete, and Verifiable example.
    – timgeb
    Dec 27 '18 at 21:52










  • My apologies for not making this a minimal example. I am new here and also to python, and I thought it would be helpful to show the full methodology in the case where I have gone about this completely wrong
    – DavidK - Computer Scientist
    Dec 27 '18 at 22:19










  • If I understand correctly, you simply need to index one more time: if i[0] is ['Apple Jacks', 'ajacks', Timestamp('2015-07-26 18:49:11'), '26-Jul-2015'], then i[0][0] is 'Apple Jacks'.
    – mkrieger1
    Dec 28 '18 at 0:15










  • Although I think I don't understand correctly, there is so much text. Can you simply edit your question to read like this: (1) this is my code, (2) this is my input, (3) this is what I get, (4) this is what I would like to get.
    – mkrieger1
    Dec 28 '18 at 0:17










  • And by "this is what I get" I mean exactly what you get copy/pasted, not described in your own words.
    – mkrieger1
    Dec 28 '18 at 0:18
















This does not feel like it's a minimal example. See Minimal, Complete, and Verifiable example.
– timgeb
Dec 27 '18 at 21:52




This does not feel like it's a minimal example. See Minimal, Complete, and Verifiable example.
– timgeb
Dec 27 '18 at 21:52












My apologies for not making this a minimal example. I am new here and also to python, and I thought it would be helpful to show the full methodology in the case where I have gone about this completely wrong
– DavidK - Computer Scientist
Dec 27 '18 at 22:19




My apologies for not making this a minimal example. I am new here and also to python, and I thought it would be helpful to show the full methodology in the case where I have gone about this completely wrong
– DavidK - Computer Scientist
Dec 27 '18 at 22:19












If I understand correctly, you simply need to index one more time: if i[0] is ['Apple Jacks', 'ajacks', Timestamp('2015-07-26 18:49:11'), '26-Jul-2015'], then i[0][0] is 'Apple Jacks'.
– mkrieger1
Dec 28 '18 at 0:15




If I understand correctly, you simply need to index one more time: if i[0] is ['Apple Jacks', 'ajacks', Timestamp('2015-07-26 18:49:11'), '26-Jul-2015'], then i[0][0] is 'Apple Jacks'.
– mkrieger1
Dec 28 '18 at 0:15












Although I think I don't understand correctly, there is so much text. Can you simply edit your question to read like this: (1) this is my code, (2) this is my input, (3) this is what I get, (4) this is what I would like to get.
– mkrieger1
Dec 28 '18 at 0:17




Although I think I don't understand correctly, there is so much text. Can you simply edit your question to read like this: (1) this is my code, (2) this is my input, (3) this is what I get, (4) this is what I would like to get.
– mkrieger1
Dec 28 '18 at 0:17












And by "this is what I get" I mean exactly what you get copy/pasted, not described in your own words.
– mkrieger1
Dec 28 '18 at 0:18




And by "this is what I get" I mean exactly what you get copy/pasted, not described in your own words.
– mkrieger1
Dec 28 '18 at 0:18












1 Answer
1






active

oldest

votes


















0














I found that the issue was with the use of the MailMerge method before the loop. It is necessary to initialize the document variable each iteration (therefore INSIDE) the loop:



from mailmerge import MailMerge
import pandas as pd

df3 = pd.DataFrame(df1, index = range(int(len(df2.index))))
looprange = range(int(len(df3.index)))

for j in looprange:
#The following line was previously before the loop. It MUST be here:
document = MailMerge(template)
document.merge(
Name= df3.name[j],
Domain= 'fruitcorp.local',
userid= df3.SamAccountName[j],
Password= '*',
date_ini='{:%d-%b-%Y}_______'.format(date.today()),
comment='* This is a replacement for a missing original form.
Original start date was: ',
startdate= df3.WhenCreatedFormated[j])
document.write(df3.SamAccountName[j] + '_Replacement_Account_Acceptance_Form.docx')





share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53951209%2fpython-how-to-iterate-through-a-dataframe-into-mailmerge-fields%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    I found that the issue was with the use of the MailMerge method before the loop. It is necessary to initialize the document variable each iteration (therefore INSIDE) the loop:



    from mailmerge import MailMerge
    import pandas as pd

    df3 = pd.DataFrame(df1, index = range(int(len(df2.index))))
    looprange = range(int(len(df3.index)))

    for j in looprange:
    #The following line was previously before the loop. It MUST be here:
    document = MailMerge(template)
    document.merge(
    Name= df3.name[j],
    Domain= 'fruitcorp.local',
    userid= df3.SamAccountName[j],
    Password= '*',
    date_ini='{:%d-%b-%Y}_______'.format(date.today()),
    comment='* This is a replacement for a missing original form.
    Original start date was: ',
    startdate= df3.WhenCreatedFormated[j])
    document.write(df3.SamAccountName[j] + '_Replacement_Account_Acceptance_Form.docx')





    share|improve this answer


























      0














      I found that the issue was with the use of the MailMerge method before the loop. It is necessary to initialize the document variable each iteration (therefore INSIDE) the loop:



      from mailmerge import MailMerge
      import pandas as pd

      df3 = pd.DataFrame(df1, index = range(int(len(df2.index))))
      looprange = range(int(len(df3.index)))

      for j in looprange:
      #The following line was previously before the loop. It MUST be here:
      document = MailMerge(template)
      document.merge(
      Name= df3.name[j],
      Domain= 'fruitcorp.local',
      userid= df3.SamAccountName[j],
      Password= '*',
      date_ini='{:%d-%b-%Y}_______'.format(date.today()),
      comment='* This is a replacement for a missing original form.
      Original start date was: ',
      startdate= df3.WhenCreatedFormated[j])
      document.write(df3.SamAccountName[j] + '_Replacement_Account_Acceptance_Form.docx')





      share|improve this answer
























        0












        0








        0






        I found that the issue was with the use of the MailMerge method before the loop. It is necessary to initialize the document variable each iteration (therefore INSIDE) the loop:



        from mailmerge import MailMerge
        import pandas as pd

        df3 = pd.DataFrame(df1, index = range(int(len(df2.index))))
        looprange = range(int(len(df3.index)))

        for j in looprange:
        #The following line was previously before the loop. It MUST be here:
        document = MailMerge(template)
        document.merge(
        Name= df3.name[j],
        Domain= 'fruitcorp.local',
        userid= df3.SamAccountName[j],
        Password= '*',
        date_ini='{:%d-%b-%Y}_______'.format(date.today()),
        comment='* This is a replacement for a missing original form.
        Original start date was: ',
        startdate= df3.WhenCreatedFormated[j])
        document.write(df3.SamAccountName[j] + '_Replacement_Account_Acceptance_Form.docx')





        share|improve this answer












        I found that the issue was with the use of the MailMerge method before the loop. It is necessary to initialize the document variable each iteration (therefore INSIDE) the loop:



        from mailmerge import MailMerge
        import pandas as pd

        df3 = pd.DataFrame(df1, index = range(int(len(df2.index))))
        looprange = range(int(len(df3.index)))

        for j in looprange:
        #The following line was previously before the loop. It MUST be here:
        document = MailMerge(template)
        document.merge(
        Name= df3.name[j],
        Domain= 'fruitcorp.local',
        userid= df3.SamAccountName[j],
        Password= '*',
        date_ini='{:%d-%b-%Y}_______'.format(date.today()),
        comment='* This is a replacement for a missing original form.
        Original start date was: ',
        startdate= df3.WhenCreatedFormated[j])
        document.write(df3.SamAccountName[j] + '_Replacement_Account_Acceptance_Form.docx')






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Dec 28 '18 at 21:27









        DavidK - Computer Scientist

        43




        43






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53951209%2fpython-how-to-iterate-through-a-dataframe-into-mailmerge-fields%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Mossoró

            Error while reading .h5 file using the rhdf5 package in R

            Pushsharp Apns notification error: 'InvalidToken'