Python:如何遍历DataFrame进入MailMerge字段?

时间:2018-12-27 21:48:20

标签: python-3.x dataframe for-loop mailmerge numpy-ndarray

问题是,即使通过循环i [0]调用的相同索引约定将正确显示,使用MailMerge方法生成的每个文件从第一次迭代到i循环都具有相同的合并字段数据。在我打印每次迭代时,以及每次循环时都使用了相同的索引引用来命名每个.docx。

代码:

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')

这是我得到的显示输出:

 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

但对

的索引调用相同
  

i [0]

在document.merge函数的'Name'字段中,对于'userid'字段的i [0]类似,导致迭代值保留为['Apple Jacks''ajacks'Timestamp('2015-07- 26 18:49:11')'2015年7月26日']每次迭代

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

相同的数据每(5)次重复一次,而不是产生5个唯一的集合 我能想到的是,当我处于MailMerge方法中时,我没有正确使用DataFrame索引。

  

...改用i [0] [0]?

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

(将我的关系数据作为字典访问会更简单吗?) 主要问题是:如何最好地使每个生成的文档包含来自循环的每次迭代的正确字段数据,而不是当前每个文档都包含第一行数据(所有生成的文档都包含:'Apple Jacks,ajacks,7-Jul -2015”(在其合并字段中)

1 个答案:

答案 0 :(得分:0)

我发现问题出在循环之前使用MailMerge方法。每次循环(因此在INSIDE之后)都必须初始化文档变量:

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')