[英]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.