Openpyxl,max_highest_column给出错误。 Worksheet对象没有属性'max_highest_column'?

时间:2017-01-11 11:25:38

标签: python openpyxl

我正在使用“使用python自动化无聊的东西”来学习python。

import openpyxl, smtplib, sys

wb = openpyxl.load_workbook('Book1.xlsx')
sheet = wb.get_sheet_by_name('Sheet1')

lastCol = sheet.max_column()
latestMonth = sheet.cell(row=1, column=lastCol).value

unpaidMember = {}
for r in range(2, sheet.get_highest_row() + 1):
    payment = sheet.cell(row=r, column=lastCol).value
    if payment != 'Y':
        name = sheet.cell(row=r, column=1).value
        email = sheet.cell(row=r, column=2).value
        unpaidMembers[name] = email


smtpObj = smtplib.SMTP('smtp-mail.outlook.com', 587)
smtpObj.ehlo()
smtpObj.starttls()
smtpObj.login('xxx@outlook.com ', 'xxxxx')

for name, email in unpaidMembers.items():
    body = "Subject: Hi \n----- \n\n -----."
    print('Sending email to %s...' % email)
    sendmailStatus = smtp0bj.sendmail('xxx@outlook.com', email, body)

    if sendmailStatus != {}:
        print('There was a problem sending email to %s: %s' % (email, sendmailStatus))

smtp0bj.quit()

上面给出了以下错误:

  

追踪(最近一次通话):    文件“C:\ Python34 \ Email Marketing.py”,第6行,in      lastCol = sheet.max_highest_column()   AttributeError:'Worksheet'对象没有属性'max_highest_column'

我环顾四周,将sheet.max_highest_column更改为sheet.max_column然后我得到以下内容:

  

追踪(最近一次通话):    文件“C:\ Python34 \ Email Marketing.py”,第6行,in      lastCol = sheet.max_column()   TypeError:'int'对象不可调用

我无法理解问题所在。 Book1保存在当前目录中,经过双重检查。

编辑:

Traceback (most recent call last):
  File "C:\Python34\Lugs and Thimbles Email Marketing.py", line 19, in     <module>
    smtp0bj = smtplib.SMTP('smtp-mail.outlook.com', 587)
  File "C:\Python34\lib\smtplib.py", line 242, in __init__
    (code, msg) = self.connect(host, port)
  File "C:\Python34\lib\smtplib.py", line 321, in connect
    self.sock = self._get_socket(host, port, self.timeout)
  File "C:\Python34\lib\smtplib.py", line 292, in _get_socket
    self.source_address)
  File "C:\Python34\lib\socket.py", line 512, in create_connection
   raise err
  File "C:\Python34\lib\socket.py", line 503, in create_connection  
    sock.connect(sa)
 TimeoutError: [WinError 10060] A connection attempt failed because the connected      party did not properly respond after a period of time, or established connection     failed because connected host has failed to respond

然后我尝试了Idle中的所有命令:

import smtplib, sys
smtp0bj = smtplib.SMTP('smtp-mail.outlook.com', 587)
smtp0bj.ehlo()
(250, b'BLU436-SMTP113.smtp.hotmail.com Hello [120.59.245.82]\nTURN\nSIZE       41943040\nETRN\nPIPELINING\nDSN\nENHANCEDSTATUSCODES\n8bitmime\nBINARYMIME\nCHUN    KING\nVRFY\nTLS\nSTARTTLS\nOK')
smtp0bj.starttls()
(220, b'2.0.0 SMTP server ready')
smtp0bj.login('xxxx@xxxx.com ', 'xxxxx')
(235, b'2.7.0 Authentication succeeded')

我在for name, email in unpaidMembers.items()之前尝试了所有代码并得到了正确的答案。

2 个答案:

答案 0 :(得分:2)

您收到错误TypeError: 'int' object is not callable,因为max_columnint,而不是函数/方法。因此,您应该lastCol = sheet.max_column(没有())。

答案 1 :(得分:0)

您正在尝试调用int(如错误所示)。 您必须使用lastCol = sheet.max_column()更改lastCol = sheet.max_column。您期望max_column值而不是对象iteself。

您还混合了UnpaidMemberUnpaidMembers