1

I think I am making a very basic mistake but I can't seem to figure it out. The error is mentioned below but I am confused because wb is defined in open_xl_file():

When I try to call the functions from another script

import openpyxl
import os
import re

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

def sheet_select():
    check = input("Have you called open_xl_file? If yes Press 1 else press 2: ")
    print(check)
    if int(check) != 1:
        open_xl_file()
        sheet = input("Which Sheet do you want to email?\n")
        wb.get_sheet_by_name(sheet)
        sheet
    else:
        sheet = input("Which State do you want to email?\n")
        wb.get_sheet_by_name(sheet)
        sheet

Error log:

NameError: name 'wb' is not defined.

1
  • Reading about scope of variables in python might help. Refer here Commented Jan 21, 2017 at 11:10

3 Answers 3

1

You need to return the open workbook with in open_xl_file: return openpyxl.load_workbook(file) and make a reference to wb in sheet_select: wb = open_xl_file():

import re, openpyxl, os


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():
    check = input("Have you called open_xl_file? If yes Press 1 else press 2: ")
    print(check)
    wb = open_xl_file()
    if int(check) != 1:
        sheet = input("Which Sheet do you want to email?\n")
        wb.get_sheet_by_name(sheet)
        return sheet
    else:
        sheet = input("Which State do you want to email?\n")
        wb.get_sheet_by_name(sheet)
        return sheet
Sign up to request clarification or add additional context in comments.

4 Comments

Ah yes. Completely forgot what I read about print and return difference and uses. :)
Slight problem with this. It makes the if pointless? Basically no matter what the input the open_xl_file() will be executed because wb is mentioned in both.
Yes, if you want to access wb, you have to open the workbook. So your if and else do exactly the same. What should be the difference between both?
Gave it a hard thought, and you are absolutely right. I wanted to check if open_xl_file() had been called and based on that remove the need to input file and path. This would in turn make me call all the functions in the next function. Example: if I want to def change_xl_file I would need to call the sheet_select function to get working on the sheet.
0

The reason is that variable wb is defined in scope of open_xl_file() function, i.e. is visible only inside this function ("local" variable). So if you want to use it outside that function you should either make it global (which is highly not recommended), or return it from your function

Comments

0

'wb' is not defined in function 'sheet_select', you need to pass 'wb' to sheet_select. You could try:

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

def sheet_select(wb):
    ...

if __name__ == '__main__':
    myWb = open_xl_file()
    sheet_select(myWb)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.