如何使用python的openpyxl模块访问单元格的真实值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22613272/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to access the real value of a cell using the openpyxl module for python
提问by user3455972
I am having real trouble with this, since the cell.value function returns the formula used for the cell, and I need to extract the result Excel provides after operating.
我遇到了真正的麻烦,因为 cell.value 函数返回用于单元格的公式,我需要在操作后提取 Excel 提供的结果。
Thank you.
谢谢你。
Ok, I think I ahve found a way around it; apparently to access cell.internal value you have to use the iter_rows() in your worksheet previously, which is a list of "RawCell".
好吧,我想我已经找到了解决方法;显然要访问 cell.internal 值,您必须之前在工作表中使用 iter_rows() ,这是“RawCell”的列表。
for row in ws.iter_rows():
for cell in row:
print cell.internal_value
回答by Vitaly Isaev
Try to use cell.internal_valueinstead.
尝试使用cell.internal_value。
回答by Charlie Clark
From the code it looks like you're using the optimised reader: read_only=True. You can switch between extracting the formula and its result by using the data_only=Trueflag when opening the workbook.
从代码看来,您正在使用优化的阅读器:read_only=True。您可以data_only=True在打开工作簿时使用标志在提取公式及其结果之间切换。
internal_valuewas a private attribute that used to refer only to the (untyped) value that Excel uses, ie. numbers with an epoch in 1900 for dates as opposed to the Python form. It has been removed from the library since this question was first asked.
internal_value是一个私有属性,用于仅引用 Excel 使用的(无类型)值,即。日期为 1900 年的数字,而不是 Python 形式。自从第一次提出这个问题以来,它已从图书馆中删除。
回答by Iulian Stana
Like Charlie Clark already suggest you can set data_onlyon Truewhen you load your workbook:
就像查理克拉克已经建议您可以data_only在True加载工作簿时进行设置:
from openpyxl import load_workbook
wb = load_workbook("file.xlsx", data_only=True)
sh = wb["Sheet_name"]
print(sh["x10"].value)
Good luck :)
祝你好运 :)
回答by Rohit Gawas
You can try following code.Just provide the excel file path and the location of the cell which value you need in terms of row number and column number below in below code.
您可以尝试以下代码。只需在下面的代码中提供excel文件路径和单元格的位置,根据行号和列号,您需要该值。
from openpyxl import Workbook
wb = Workbook()
Dest_filename = 'excel_file_path'
ws=wb.active
print(ws.cell(row=row_number, column=column_number).value)

