python的openpyxl库如何读取特定列,Openpyxl-如何在Python中从Excel文件中仅读取一列?...

I want to pull only column A from my spreadsheet. I have the below code, but it pulls from all columns.

from openpyxl import Workbook, load_workbook

wb=load_workbook("/home/ilissa/Documents/AnacondaFiles/AZ_Palmetto_MUSC_searchterms.xlsx", use_iterators=True)

sheet_ranges=wb['PrivAlert Terms']

for row in sheet_ranges.iter_rows(row_offset=1):

for cell in row:

print(cell.value)

解决方案

this is an alternative to previous answers in case you whish read one or more columns using openpyxl

import openpyxl

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

first_sheet = wb.get_sheet_names()[0]

worksheet = wb.get_sheet_by_name(first_sheet)

#here you iterate over the rows in the specific column

for row in range(2,worksheet.max_row+1):

for column in "ADEF": #Here you can add or reduce the columns

cell_name = "{}{}".format(column, row)

worksheet[cell_name].value # the value of the specific cell

... your tasks...

I hope that this be useful.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值