Python - 验证我的文档xls中是否存在工作表

Lom*_*man 5 python xls

我正在尝试在我的空闲时间设计一个小程序,它加载一个xls文件,然后在要扫描的文档中选择一个工作表.

第1步:用户导入.xls文件.导入程序后,检查文件是否存在.(我可以做)

第2步:我要求用户提供要分析的文档表xls的名称.这就是它停止的地方.该程序没有检测到可用的床单:(

#check if the document exist
while True:
    x = input("Give the name of the document in this repository__")
    input_filename = x + ".xls"
    if os.path.isfile(input_filename):
        print ("the document is been charged")
        break
    else:
        print("xls not found !")

#Load the document
xls_file = pd.ExcelFile(input_filename)

#Select the good sheet in file
print ("this is your sheets in this document",xls_file.sheet_names)
while True:
    feuilles = input("Select yout sheet")
    input_feuilles = feuilles
    if xls_file.sheet_names(input_filename):
        print ("The sheet is been charged !")
        break
    else:
        print("This sheet don't exist!")
Run Code Online (Sandbox Code Playgroud)

我真的不知道如何验证用户填写的表格确实存在.

Spa*_*ine 12

Python库openpyxl专为读取和写入Excel xlsx/xlsm/xltx/xltm文件而设计.以下代码段代码检查给定工作簿中是否存在特定工作表名称.

PS:对于较旧的Microsoft Excel文件(即.xls),请使用xlrdxlwt不是.

from openpyxl import load_workbook

wb = load_workbook(file_workbook, read_only=True)   # open an Excel file and return a workbook

if 'sheet1' in wb.sheetnames:
    print('sheet1 exists')
Run Code Online (Sandbox Code Playgroud)

openpyxl使用以下命令安装.

$ sudo pip install openpyxl
Run Code Online (Sandbox Code Playgroud)