下記のxlsxのデータを読み込みメールで送信したいですが、
AttributeErrorのエラーが表示されます。
以下のエラーはどう直せばよいのでしょうか。
xlsxのデータ
A B C
アメリカ 1234個 66.7%
カナダ 1234個 4.9%
エラー
['アメリカ 1234 66.7%']
['カナダ' 1234 4.9%']
Traceback (most recent call last):
File "c:/Users/test/Documents/test/test.py", line 28, in <module>
message = '\n'.join([f"{column}\n{data}\n" for column,data in row.iloc[values].iteritems()]) #error !
AttributeError: 'tuple' object has no attribute 'iloc'
code
import openpyxl
from openpyxl.utils import column_index_from_string
from smtplib import SMTP
from email.mime.text import MIMEText
from email import encoders
from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart
import smtplib
import codecs
import pandas as pd
wb = openpyxl.load_workbook('text.xlsx',data_only=True)
ws = wb.worksheets[0]
for row in ws["A1:C2"]:
values = []
for col in row:
values.append(col.value)
print(values)
account = "1223"
password = "1223"
to_email = "1223"
from_email = "1223"
subject = "Test"
message = '\n'.join([f"{column}\n{data}\n" for column,data in row.iloc[values].iteritems()]) #error !
#'\n'.join(values)
print(message)
msg = MIMEMultipart()
msg["Subject"] = subject
msg["To"] = to_email
msg["From"] = from_email
# メール本文
message = MIMEText(message)
msg.attach(message)
# 添付ファイルの設定
attach_file = {'name': 'test.jpeg', 'path': 'C:\\Users\\test\\Desktop\\test.jpeg'} # nameは添付ファイル名。pathは添付ファイルの位置を指定
attachment = MIMEBase('image', 'jpeg')
file = open(attach_file['path'], 'rb+')
attachment.set_payload(file.read())
file.close()
encoders.encode_base64(attachment)
attachment.add_header("Content-Disposition", "attachment", filename=attach_file['name'])
msg.attach(attachment)
server = smtplib.SMTP("smtp.gmail.com", 587)
server.starttls()
server.login(account, password)
server.send_message(msg)
server.quit()
28行目のmessageを下記に直すとエラーなしで行きますが、
出力結果 カナダ 1234 4.9%
1行しか取得できないです。
#'\n'.join(values)
わかる方いらっしゃいましたらご教示願います。
お手数ですが、宜しくお願いいたします。