python excel 邮件合并?
时间: 2023-11-20 17:58:12 浏览: 193
python中excel合并
根据提供的引用内容,介绍Python实现Excel邮件合并的方法:
1. Python实现方法1:
```python
import win32com.client as win32
import pandas as pd
# 读取Excel数据
df = pd.read_excel('data.xlsx')
# 邮件合并
for index, row in df.iterrows():
outlook = win32.Dispatch('Outlook.Application')
mail = outlook.CreateItem(0)
mail.To = row['收件人']
mail.Subject = row['主题']
mail.Body = row['正文']
attachment = row['附件']
mail.Attachments.Add(attachment)
mail.Send()
```
2. Python实现方法2:
```python
import openpyxl
from openpyxl.utils import get_column_letter
from openpyxl.utils.cell import column_index_from_string
from openpyxl.styles import Font
import os
import win32com.client as win32
# 读取Excel数据
wb = openpyxl.load_workbook('data.xlsx')
sheet = wb.active
# 邮件合并
for row in range(2, sheet.max_row + 1):
outlook = win32.Dispatch('Outlook.Application')
mail = outlook.CreateItem(0)
mail.To = sheet.cell(row=row, column=column_index_from_string('B')).value
mail.Subject = sheet.cell(row=row, column=column_index_from_string('C')).value
mail.Body = sheet.cell(row=row, column=column_index_from_string('D')).value
attachment = sheet.cell(row=row, column=column_index_from_string('E')).value
mail.Attachments.Add(attachment)
mail.Send()
```
3. Python实现方法3:
```python
import xlrd
import win32com.client as win32
# 读取Excel数据
workbook = xlrd.open_workbook('data.xlsx')
sheet = workbook.sheet_by_index(0)
# 邮件合并
for row in range(1, sheet.nrows):
outlook = win32.Dispatch('Outlook.Application')
mail = outlook.CreateItem(0)
mail.To = sheet.cell_value(row, 1)
mail.Subject = sheet.cell_value(row, 2)
mail.Body = sheet.cell_value(row, 3)
attachment = sheet.cell_value(row, 4)
mail.Attachments.Add(attachment)
mail.Send()
```
阅读全文