python:在xlrd中获取活动表?以及在Python中读取和验证excel文件的帮助 [英] python : Get Active Sheet in xlrd? and help for reading and validating excel file in Python

查看:577
本文介绍了python:在xlrd中获取活动表?以及在Python中读取和验证excel文件的帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

2个要问的问题:

问题1: 我刚刚开始研究 xlrd 以便在python中读取excel文件. 我想知道xlsrd->中是否有一种方法类似于 openpyxl 中的get_active_sheet()或通过其他任何方式获取活动表?

Ques 1: I just started studying about xlrd for reading excel file in python. I was wondering if there is a method in xlsrd --> similar to get_active_sheet() in openpyxl or any other way to get the Active sheet ?

get_active_sheet()openpyxl


import openpyxl

wb = openpyxl.load_workbook('example.xlsx')

active_sheet = wb.get_active_sheet()


输出:工作表"Sheet1"

output : Worksheet "Sheet1"

我在xlrd中找到了用于检索工作表名称的方法,但是没有一个方法可以告诉我活动工作表.

I had found methods in xlrd for retrieving the names of sheets, but none of them could tell me the active sheet.

问题2:

xlrd是python中用于读取Excel文件的最佳打包软件吗?我还遇到了,其中包含有关其他python软件包的信息(xlsxwriter xlwt xlutils )以读取和写入excel文件.

Is xlrd the best packaage in python for reading excel files? I also came across this which had info about other python packages(xlsxwriterxlwtxlutils) for reading and writing excel files.

其中最适合用于制作读取Excel文件并将不同的验证应用于不同列的应用程序

Which of the above all will be best for making an App which reads an Excel File and applies different validations to to different columns

例如:标题为"ID"的列应具有唯一值,标题为"Country"的列应具有有效的国家/地区.

For eg: Column with Header 'ID' should have unique values and A column with Header 'Country' should have valid Countries.

推荐答案

这里的活动工作表"似乎是指保存/关闭工作簿时选择的最后一个工作表.您可以通过sheet_visible值获取此工作表.

The "active sheet" here seems you're referring to the last sheet selected when the workbook was saved/closed. You can get this sheet via sheet_visible value.

https://github.com/python -excel/xlrd/blob/master/xlrd/sheet.py#L33

import xlrd
xl = xlrd.open_workbook("example.xls")
for sht in xl.sheets():
    # sht.sheet_visible value of 1 is "active sheet"
    print(sht.name, sht.sheet_selected, sht.sheet_visible)

通常一次只选择一张图纸,因此看起来sheet_visiblesheet_selected是相同的,但是一次可以选择多张图纸(例如,按住ctrl键并单击多个图纸标签).

Usually only one sheet is selected at a time, so it may look like sheet_visible and sheet_selected are the same, but multiple sheets can be selected at a time (ctrl+click multiple sheet tabs, for example).

这似乎令人困惑的另一个原因是因为Excel在隐藏/可见工作表方面使用了可见".在xlrd中,这是sheet.visibility(请参见 https://stackoverflow.com/a/44583134/4258124 )

Another reason this may seem confusing is because Excel uses "visible" in terms of hidden/visible sheets. In xlrd, this is instead sheet.visibility (see https://stackoverflow.com/a/44583134/4258124)

这篇关于python:在xlrd中获取活动表?以及在Python中读取和验证excel文件的帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆