定义几个函数,由于绑定方法而无法获取工作表名称?

时间:2017-01-29 16:28:34

标签: python python-3.x openpyxl

通过实践学习python。

代码:

import re, openpyxl, os
from openpyxl import load_workbook


def open_xl_file():
    loc = input("Please enter path of the file:")
    os.chdir(loc)
    file = input("Filename:")
    return openpyxl.load_workbook(file)

def sheet_select():
    wb = open_xl_file()
    print(wb.get_sheet_names)
    y = input("Which sheet?\n")
    return wb.get_sheet_by_name(y)

我将print(wb.get_sheet_names)上的输出视为:

<bound method Workbook.get_sheet_names of <openpyxl.workbook.workbook.Workbook object at 0x0000000004E3F2E8>>

试图找出我搞砸的地方。我已经尝试在第一个函数中包含wb并返回它。

1 个答案:

答案 0 :(得分:1)

正如有些人在评论中提到的那样,您应该print(wb.get_sheet_names())而不是print(wb.get_sheet_names)