openpyxl, max_highest_column 报错。工作表对象没有属性 'max_highest_column'?

Openpyxl, max_highest_column gives an error. Worksheet object has no attribute 'max_highest_column'?

我正在学习 python 使用 "automate the boring stuff with 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()

以上给出了以下错误:

Traceback (most recent call last): File "C:\Python34\Email Marketing.py", line 6, in lastCol = sheet.max_highest_column() AttributeError: 'Worksheet' object has no attribute 'max_highest_column'

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

Traceback (most recent call last): File "C:\Python34\Email Marketing.py", line 6, in lastCol = sheet.max_column() TypeError: 'int' object is not callable

我无法理解问题所在。 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() 之前的所有代码并得到了正确的响应。

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

您正在尝试调用一个 int(如错误所述)。 您必须将 lastCol = sheet.max_column() 更改为 lastCol = sheet.max_column。您期望的是 max_column 值而不是对象本身。

你也在混合 UnpaidMemberUnpaidMembers