简体   繁体   English

如何使用python从文件夹中的多个excel文件中读取具有“ mine”工作表名称的工作表? 我正在使用xlrd

[英]how to read any sheet with the sheet name containing 'mine' from multiple excel files in a folder using python? i am using xlrd

How to read any sheet with the sheet name containing 'mine' from multiple excel files in a folder using python? 如何使用python从文件夹中的多个excel文件中读取工作表名称包含“ mine”的工作表? I am using xlrd. 我正在使用xlrd。

dir_to_load = "C:\\Users\\User\\Desktop\\"
dir_to_save = "C:\\Users\\User\\Desktop\\"
file_name = "Test.xlsx"

os.chdir(dir_to_load)

wb= xlrd.open_workbook(file_name)
ws = wb.sheet_by_index(-1)
out_list = []

I know I am only reading one file and getting the last sheet but I want the sheets containing or LIKE %Mine% from multiple files. 我知道我只读取一个文件并获取最后一张纸,但是我希望纸中包含或喜欢来自多个文件的%Mine%。 Thanks. 谢谢。

Here is a solution that is based around using a function, get_worksheet_names(spreadsheet_name) , to get the names of each worksheet in a spreadsheet: 这是一个基于使用功能get_worksheet_names(spreadsheet_name)的解决方案,以获取get_worksheet_names(spreadsheet_name)中每个工作表的名称:

import xlrd 

# Utility function to get worksheet names from a spreadsheet:
def get_worksheet_names(spreadsheet_name):
    worksheets = xlrd.open_workbook(spreadsheet_name).sheet_names()

    worksheet_names = []
    for j in worksheets:
        worksheet_names.append(worksheets.sheet_by_name(j))

    return worksheet_names  


# Here is your original code, modified:
dir_to_load = "C:\\Users\\User\\Desktop\\"
dir_to_save = "C:\\Users\\User\\Desktop\\"
file_name = "Test.xlsx"

os.chdir(dir_to_load)

wsnames = get_worksheet_names(file_name)

for name in wsnames:
    if 'mine' in name: # change to whatever search criteria you'd like here
        print name

(Note on terminology: Microsoft uses "workbook" and "spreadsheet" interchangeably , but I prefer "spreadsheet" as everyone knows what that means, whereas "workbook" is more ambiguous.) (有关术语的注释:Microsoft 交替使用“工作簿”和“电子表格” ,但我更喜欢“电子表格”,因为每个人都知道这意味着什么,而“工作簿”则更加含糊。)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用xlrd在Python中获取Excel工作表名称 - How to get excel sheet name in Python using xlrd 如何使用pythons xlrd模块从Excel工作表中读取 - How to read from an excel sheet using pythons xlrd module 我在 python 中使用 Pandas 来读取 csv,如何传递工作表名称来读取特定工作表 - I am using pandas in python to read a csv,how do I pass a sheet name to read particular sheet 使用xlrd以时间格式而不是浮点数从excel表中读取时间 - Read time from excel sheet using xlrd, in time format and not in float 使用XLRD在python 3中读取Excel工作表的问题 - Issue reading an excel sheet in python 3 using XLRD 使用Excel工作表中的名称映射,借助Python Script重命名文件夹中文件的名称 - Rename the name of files in a folder with the help of Python Script, using name map from Excel sheet 使用Python工作表中的名称映射,借助Python脚本重命名文件夹中的文件名 - Rename the name of files in the folder with the help of Python Script, using name map from Excel sheet 使用python从文件夹中的多个文本文件中提取特定值并将其存储在Excel工作表中 - Extracting specific value from multiple text files from folder using python and store it in Excel sheet 如何从多个 csv 文件中读取数据并写入 Python 中的单个 Excel 表的同一张表 - How to read data from multiple csv files and write into same sheet of single Excel Sheet in Python 使用python和xlrd计算Excel工作表中特定单词的出现次数 - Count number of occurences of specific words in Excel sheet using python & xlrd
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM