使用 openpxl 和 python 导入 xlsx 文件

Using openpxl and python to import a xlsx file

我有多个 sheet excel 工作簿,我想从工作簿中的 sheet 中导入两列,我该怎么做?我一直在网上四处寻找,但无处可寻。我正在尝试使用包 openpyxl。

你可以关注这个:

from openpyxl import *

#import the excel workbook
wb = load_workbook('input.xlsx')
#this should give you the sheet names
WSheets = wb.sheetnames

#iterate through sheets
for i in WSheets:
    ws = wb[i]
    #To access values from a particular column of the sheet, for example column B
    print ws['B']

应该给

(<Cell vane.B1>, <Cell vane.B2>, <Cell vane.B3>, <Cell vane.B4>, <Cell vane.B5>, <Cell vane.B6>, <Cell vane.B7>, <Cell vane.B8>, <Cell vane.B9>, <Cell vane.B10>, <Cell vane.B11>, <Cell vane.B12>, <Cell vane.B13>, <Cell vane.B14>, <Cell vane.B15>, <Cell vane.B16>, <Cell vane.B17>, <Cell vane.B18>, <Cell vane.B19>, <Cell vane.B20>, <Cell vane.B21>, <Cell vane.B22>, <Cell vane.B23>, <Cell vane.B24>, <Cell vane.B25>, <Cell vane.B26>, <Cell vane.B27>, <Cell vane.B28>, <Cell vane.B29>)

等等...

注意:我使用的是 openpyxl 2.4.0-a1 早期版本可能有不同的语法。