如何打印sheet索引内容?
How to print sheet index content?
我的代码
from __future__ import print_function
import xlrd
workbook = xlrd.open_workbook("/home/jh/Downloads/N9140US2m.xls")
sheet_names = workbook.sheet_names()
sheet_by_index = workbook.sheet_by_index(0)
print('Sheet Names', sheet_names)
print('Sheet Index', sheet_by_index)
输出
Sheet Names ['Contents', 'Data 1']
Sheet Index <xlrd.sheet.Sheet object at 0x7fb004611240>
我想打印 sheet 对象。
这是 xls 文件的样子
如果我采用 Ashinish 建议的解决方案
print("{0} {1} {2}".format(sh.name, sh.nrows, sh.ncols))
比输出
Contents 15 6
编辑
workbook = xlrd.open_workbook("/home/milenko/Downloads/N9140US2m.xls")
sheet_names = workbook.sheet_names()
sheet_by_index = workbook.sheet_by_index(0)
second_column = sheet_by_index.col_values(1, start_rowx=3)
print('Second column', second_column)
输出
Second column ['', 'Click worksheet name or tab at bottom for data', 'Worksheet Name', 'Data 1', '', 'Release Date:', 'Next Release Date:', 'Excel File Name:', 'Available from Web Page:', 'Source:', 'For Help, Contact:', '']
要获取第二列的所有内容,请使用 col_values
:
second_column = sheet_by_index.col_values(1, start_rowx=3)
第一个参数指定从0
开始的那一列。所以 1
将是第二列。
第二个参数start_rowx
指定需要读取内容的起始行。 start_rowx=3
表示从行 4
开始(因为,我发现您的 sheet 的实际值从行 4
开始)。
我的代码
from __future__ import print_function
import xlrd
workbook = xlrd.open_workbook("/home/jh/Downloads/N9140US2m.xls")
sheet_names = workbook.sheet_names()
sheet_by_index = workbook.sheet_by_index(0)
print('Sheet Names', sheet_names)
print('Sheet Index', sheet_by_index)
输出
Sheet Names ['Contents', 'Data 1']
Sheet Index <xlrd.sheet.Sheet object at 0x7fb004611240>
我想打印 sheet 对象。
这是 xls 文件的样子
如果我采用 Ashinish 建议的解决方案
print("{0} {1} {2}".format(sh.name, sh.nrows, sh.ncols))
比输出
Contents 15 6
编辑
workbook = xlrd.open_workbook("/home/milenko/Downloads/N9140US2m.xls")
sheet_names = workbook.sheet_names()
sheet_by_index = workbook.sheet_by_index(0)
second_column = sheet_by_index.col_values(1, start_rowx=3)
print('Second column', second_column)
输出
Second column ['', 'Click worksheet name or tab at bottom for data', 'Worksheet Name', 'Data 1', '', 'Release Date:', 'Next Release Date:', 'Excel File Name:', 'Available from Web Page:', 'Source:', 'For Help, Contact:', '']
要获取第二列的所有内容,请使用 col_values
:
second_column = sheet_by_index.col_values(1, start_rowx=3)
第一个参数指定从0
开始的那一列。所以 1
将是第二列。
第二个参数start_rowx
指定需要读取内容的起始行。 start_rowx=3
表示从行 4
开始(因为,我发现您的 sheet 的实际值从行 4
开始)。