2016-10-22 48 views
0

我是一名編程新手,我的一半問題是我找不到正確的問題要問 - 我看了很多堆棧溢出帖子,試圖解決我的問題,但我無法將我發現的情況應用於我的情況。因此,我需要你的幫助,互聯網。通過電子郵件發送使用Python3的銷售清單

我試圖做一個程序,每週運行一次使用Windows調度程序。當它運行時,它應該:

  • 請教一個Excel文件,
  • 提取近四行的一些信息,
  • 繪製了一個表格,電子郵件,
  • 包括形式的電子郵件中提取的信息,
  • 發送表格電子郵件給特定的收件人。

我有SMTP做工精細,包括形式電子郵件生成,而且我可以得到統計出使用openpyxl Excel工作表的,但我不能搏鬥的信息轉換成可用的格式發送。

到目前爲止,我已經得到了(用於處理信息)的代碼如下所示:

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

我從得到的是文本的整體令,就像這樣:

['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]

理想我想在一個電子郵件發送是這樣,對每個單獨的銷售項目的新行:

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

我寫了一封電子郵件,體,其設置爲包括使用的格式類似下面的信息列表:

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

任何指針上無疑是可怕的代碼上面會受到歡迎。

+0

看看使用「」。加入(recentSales)把列表轉換爲字符串。請看最新的openpyxl文檔。 –

回答

-1

我相信像這樣會產生你所需要的東西。

for ls in recentList: 
    line = " ".join([str(x) for x in ls]) 
    print(line + "\n") 
+0

列表中的元素將自動轉換爲字符串,因此不需要嵌套理解。 –

+0

@CharlieClark '>>> MYLIST [ '戴頓,萊恩(生於1929年)。', '一閃一閃小間諜',20] >>> 「」。加入(MYLIST) 回溯(最近通話最後): 文件「」,第1行,在 TypeError:序列項目2:期望的字符串,找到int# – user3468054

+1

@CharlieClark [python文檔](https://docs.python.org/3/library/stdtypes。 html?highlight = join#str.join)表示它只接受字符串 –

0

正如其他人所說,使用加入。

你可以,代替 recentList.append(recentSales) 做這樣的事情: recentList.append(' '.join([str(x) for x in recentSales) 這將導致串的recentList一個很好的格式列表。

線產生那麼你的電子郵件正文可能是這樣的:

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