大家早上好,
我编写了一个脚本,该脚本使用多个字段(运至#、序列号等)填充 word 文档字母,并为我的地址列表中的每一行创建一个新的 word 文档,其中包含运至#、序列号和客户名称。我的代码为地址列表中的每个行条目输出所有 2000 个字母。但是,一位客户可能有多个序列号,因此地址列表中可能有多个条目。当前代码为每个不同的行/序列号打印出同一客户的多个字母。
如何在同一个字母中为每个不同的序列号添加一行,而不是为每个不同的序列号为同一客户添加多个字母?我目前正在尝试使用重复的客户名称及其相应的序列号创建一个字典,然后在 mailmerge 中使用这个字典,但仍然不确定如何。非常感谢任何提示或指导。
EDIT1:当我尝试使用 this 解析重复项时print ([item for item, count in collections.Counter(dict_list).items() if count > 1])
,我收到一条错误消息:unhashable type: 'dict'。
from __future__ import print_function
from xlrd import open_workbook
from mailmerge import MailMerge
import pandas as pd
import numpy as np
import operator
import itertools
#####################address list template
book = open_workbook('Template3.xlsx')
sheet = book.sheet_by_index(0)
keys = [sheet.cell(0, col_index).value for col_index in range(sheet.ncols)]
dict_list = []
for row_index in range(1, sheet.nrows):
d = {keys[col_index]: sheet.cell(row_index, col_index).value
for col_index in range(sheet.ncols)}
dict_list.append(d)
###################serial number template
book = open_workbook('serialtemp1.xlsx')
sheet = book.sheet_by_index(0)
serialkeys = [sheet.cell(0, col_index).value for col_index in range(sheet.ncols)]
serial_list = []
for row_index in range(1, sheet.nrows):
d1 = {serialkeys[col_index]: sheet.cell(row_index, col_index).value
for col_index in range(sheet.ncols)}
serial_list.append(d1)
#serialized = map(tuple, map(sorted, map(dict.items, serial_list)))
#unique = set(serialized)
#result = list(map(dict, unique))
#print(serial_list)
######################new dict with serial as keys
key = operator.itemgetter('Ship To')
b = [{'Ship To': x, 'serial_number': str({d1['serial_number'] for d1 in y})}
for x, y in itertools.groupby(sorted(serial_list, key=key), key=key)]
###################create pandas df for serial list
pdframe1 = pd.DataFrame(b)
pdframe1.sort_values(by=['Ship To'])
pdframe1['serial_number']=pdframe1['serial_number'].str.replace(r"\{","")
pdframe1['serial_number']=pdframe1['serial_number'].str.replace(r"\'","")
pdframe1['serial_number']=pdframe1['serial_number'].str.replace(r"\}","")
pdframe1['Ship To'] = pdframe1['Ship To'].astype(np.int64)
pdframe1['serial_number'] = pdframe1['serial_number'].astype(str).replace('\.0', '', regex=True)
##############################33
pdframe = pd.DataFrame(dict_list)
pdframe.sort_values(by=['Ship To'])
pdframe['serial_number'] = pdframe['serial_number'].astype(str).replace('\.0', '', regex=True)
pdframe['Ship To'] = pdframe['Ship To'].astype(np.int64)
############################
pdframe = pdframe.merge(pdframe1, on = 'Ship To', how = 'right')
pdframe.sort_values(by=['Ship To'])
pdframe = pdframe.astype(str)
############3
looprange1 = range(len(pdframe1.index))
looprange = range(int(len(pdframe.index)))
for j in looprange:
a =[{'serial_number' : pdframe1['serial_number'][k]} for k in looprange1]
template = 'Template.docx'
document = MailMerge(template)
document.merge(
ship_to1 = pdframe['Ship To'][j],
mailing_name1 = pdframe['Mailing Name'][j],
address_line11 = pdframe['Address Line 1'][j],
address_line21 = pdframe['Address Line 2'][j],
city1 = pdframe['City'][j],
state1 = pdframe['State'][j],
zipcode1 = pdframe['ZipCode'][j],
ship_to2 = pdframe['Ship To'][j],
mailing_name2 = pdframe['Mailing Name'][j],
address_line12 = pdframe['Address Line 1'][j],
address_line22 = pdframe['Address Line 2'][j],
city2 = pdframe['City'][j],
state2 = pdframe['State'][j],
zipcode2 = pdframe['ZipCode'][j])
document.merge_rows('serial_number', a)
document.write(pdframe['Mailing Name'][j]+ ' ' + pdframe['Ship To'][j] +'.docx')