使用Python3发送销售列表 [英] Email a list of sales using Python3

查看:126
本文介绍了使用Python3发送销售列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个编程新手,我的一半问题是找不到正确的问题 - 我看过很多Stack Overflow帖子来尝试解决我的问题,但是我还没有应用我发现的情况。因此,我需要你的帮助,互联网。

I am a programming newbie, and half of my problem is that I cannot find the right questions to ask - I have looked at lots of Stack Overflow posts to try to work through my issues, but I've not been able to apply what I've found to my situation. Consequently, I need your help, internet.

我正在尝试使用Windows Scheduler每周运行一次程序。当它运行时,应该:

I'm trying to make a program that runs once a week using Windows Scheduler. When it runs, it should:


  • 查看excel文件,

  • 从最后四十行

  • 起草一份表单电子邮件,

  • 以电子邮件形式包含提取的信息,

  • 发送表单邮件给特定的收件人。

  • consult an excel file,
  • extract some information from the last forty rows,
  • draw up a form email,
  • include the extracted information in the form email,
  • send the form email to a specific recipient.

我已经有 SMTP 表单电子邮件生成器,我可以使用 openpyxl 获取Excel表格中的统计信息,但是我无法将信息作为可用格式发送。

I've got SMTP working fine, including the form email generator, and I can get the stats out of the excel sheet using openpyxl, but I cannot wrestle the information into a useable format to send.

我到目前为止(用于处理该信息)的代码如下所示:

The code I've got so far (for handling that info) looks like this:

# Open stats sheet
wb = openpyxl.load_workbook('Stats.xlsx')
sheet = wb.get_sheet_by_name('DATA')

# Get the author, title and price of last forty sales
ultimateRow = sheet.max_row + 1
limitRow = sheet.max_row - 40
recentList = []
for row in range(limitRow, ultimateRow):
    recentSales = []
    for column in 'GHI':
        cell_name = '{}{}'.format(column, row)
        recentSales.append(sheet[cell_name].value)
    recentList.append(recentSales)

print(*recentList)

我从中得到的是一个完整的文字,如下所示:

What I get from that is a whole ream of text, like this:


['DEIGHTON ,Len(1929年出生),Twinkle Twinkle Little Spy,20] ['BROOKE,Rupert(1887-1915); ABERCROMBIE,Lascelles(1881-1938);饮料,约翰(1882-1937); GIBSON,Wilfrid Wilson(1878-1962),新数字第1卷第3期,76] ['SHUTE,Nevil。',Aly Like Alice,100] ['SWINBURNE,Algernon Charles -1909),意大利之歌,15]

['DEIGHTON, Len (born 1929).', 'Twinkle Twinkle Little Spy.', 20] ['BROOKE, Rupert (1887-1915); ABERCROMBIE, Lascelles (1881-1938); DRINKWATER, John (1882-1937); GIBSON, Wilfrid Wilson (1878-1962).', 'New Numbers Volume 1 Number 3.', 76] ['SHUTE, Nevil.', 'A Town Like Alice.', 100] ['SWINBURNE, Algernon Charles (1837-1909).', 'A Song of Italy.', 15]

理想情况下,我想在电子邮件中发送什么看起来像这样,每个销售项目的新行:

Ideally what I would want to send in an email would look like this, with a new line for each individual sale item:


DEIGHTON,Len(生于1929年)。 - Twinkle Twinkle Little Spy.- 20

DEIGHTON, Len (born 1929). - Twinkle Twinkle Little Spy.- 20

BROOKE,Rupert(1887-1915); ABERCROMBIE,Lascelles(1881-1938);饮料,约翰(1882-1937); GIBSON,Wilfrid Wilson(1878-1962) - 新的数字第1卷第3 - 76页

BROOKE, Rupert (1887-1915); ABERCROMBIE, Lascelles (1881-1938); DRINKWATER, John (1882-1937); GIBSON, Wilfrid Wilson (1878-1962) - New Numbers Volume 1 Number 3. - 76

我写了一个电子邮件正文,其设置包括使用以下格式的信息列表:

I've written an email body, which is set up to include the list of info using a format like the following:

body = ''' This is an email. Here's the list: {}'''.format(list)

任何指针无疑是可怕的上面的代码将不胜感激。

Any pointers on the no doubt hideous code above would be gratefully received.

推荐答案

正如其他人所说,使用join。

As others have said, use join.

您可以代替
recentList.append(recentSales)
做某事喜欢:
recentList.append(''.join([str(x)for recent in sales)
这将导致一个很好的格式 recentList 中的字符串列表。

You could, in place of recentList.append(recentSales) do something like: recentList.append(' '.join([str(x) for x in recentSales) which will result in a nicely-formatted list of strings in recentList.

生成电子邮件正文的行可以是:

The line to generate your email body could then be something like:

>>> body = 'This is an email. Here is the list\n' + '\n'.join('{}'.format(l) for l in recentList)
>>> print body
This is an email. Here is the list
DEIGHTON, Len (born 1929). Twinkle Twinkle Little Spy. 20
BROOKE, Rupert (1887-1915); ABERCROMBIE, Lascelles (1881-1938); DRINKWATER, John (1882-1937); GIBSON, Wilfrid Wilson (1878-1962). New Numbers Volume 1 Number 3. 76
SHUTE, Nevil. A Town Like Alice. 100
SWINBURNE, Algernon Charles (1837-1909). A Song of Italy. 15

这篇关于使用Python3发送销售列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆