Openpyxl-如何在Python中从Excel文件中仅读取一列? [英] Openpyxl - How to read only one column from Excel file in Python?
本文介绍了Openpyxl-如何在Python中从Excel文件中仅读取一列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我只想从电子表格中提取列A.我有下面的代码,但是它是从所有列中提取的.
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)
推荐答案
如果您希望使用openpyxl阅读一列或多列,这是先前答案的替代方法
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...
我希望这会有用.
这篇关于Openpyxl-如何在Python中从Excel文件中仅读取一列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文