- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在寻找一种使用openpyxl
读取一系列单元格的更好(更具可读性/更少被组合在一起)的方法。我目前所拥有的可以工作,但涉及通过组装字符串的位来组成 Excel 单元格范围(例如 A1:C3
),这感觉有点粗糙。
目前,这就是我从特定单元格开始读取 nCols
列和 nRows
行的方式(最小工作示例,假设 worksheet.xlsx
位于工作目录中,并且在 Sheet1
中的单元格 A1
到 C3
中写入单元格引用:
from openpyxl import load_workbook
import numpy as np
firstCol = "B"
firstRow = 2
nCols = 2
nRows = 2
lastCol = chr(ord(firstCol) + nCols - 1)
cellRange = firstCol + str(firstRow) + ":" + lastCol + str(firstRow + nRows - 1)
wsName = "Sheet1"
wb = load_workbook(filename="worksheet.xlsx", data_only=True)
data = np.array([[i.value for i in j] for j in wb[wsName][cellRange]])
print(data)
返回:
[[u'B2' u'C2']
[u'B3' u'C3']]
这种方法除了有点难看之外,还存在功能限制。例如,在超过 26 列的工作表中,对于像 AA
这样的列,它将失败。
是否有更好/正确的方法使用 openpyxl 从给定的左上角读取 nRows
和 nCols
?
最佳答案
openpyxl 提供了在数字列索引(基于 1 的索引)和 Excel 的“AA”样式之间进行转换的函数。有关详细信息,请参阅 utils
模块。
但是,一般来说您不需要它们。您可以使用工作表的 get_squared_range()
方法进行编程访问。并且,从 openpyxl 2.4 开始,您可以使用 iter_rows()
和 iter_cols()
方法执行相同的操作。注意。 iter_cols()
在只读模式下不可用。
使用 iter_rows()
的等效 MWE 为:
from openpyxl import load_workbook
import numpy as np
wsName = "Sheet1"
wb = load_workbook(filename="worksheet.xlsx", data_only=True)
ws = wb[wsName]
firstRow = 2
firstCol = 2
nCols = 2
nRows = 2
allCells = np.array([[cell.value for cell in row] for row in ws.iter_rows()])
# allCells is zero-indexed
data = allCells[(firstRow-1):(firstRow-1+nRows),(firstCol-1):(firstCol-1+nCols)]
print(data)
使用 get_squared_range()
的等效 MWE 为:
from openpyxl import load_workbook
import numpy as np
wsName = "Sheet1"
wb = load_workbook(filename="worksheet.xlsx", data_only=True)
firstCol = 2
firstRow = 2
nCols = 2
nRows = 2
data = np.array([[i.value for i in j] for j in wb[wsName].get_squared_range(
firstCol, firstRow, firstCol+nCols-1, firstRow+nRows-1)])
print(data)
两者都返回:
[[u'B2' u'C2']
[u'B3' u'C3']]
另请参阅https://openpyxl.readthedocs.io/en/default/pandas.html有关一起使用 Pandas 和 openpyxl 的更多信息。
关于python - openpyxl:将一系列数字读取到数组的更好方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39968466/
有没有办法使用 openpyxl 折叠多行?似乎没有与 openpyxl simple usage page 上的示例等效的行。 import openpyxl wb = openpyxl.Workb
我有一个应用程序,如果已经有数据,我将工作表写入最后一列 + 2,如果工作表为空,则写入最后一列 + 1。我得到了我认为是空的工作表,如下所示: from openpyxl.workbook.work
我有一个应用程序,如果已经有数据,我将工作表写入最后一列 + 2,如果工作表为空,则写入最后一列 + 1。我得到了我认为是空的工作表,如下所示: from openpyxl.workbook.work
当我从 openpyxl-2.5.12 升级到 openpyxl-3.0.3 时,我开始收到以下错误: C:\workspace\venv_py37_64\lib\site-packages\open
我可以使用以下代码创建垂直条形图: from openpyxl import Workbook wb = Workbook() ws = wb.active for i in range(10):
我对 openpyxl 库有疑问。 问题: 当我使用 xlwt,打开一个 .xls 文件,并在其中写入一些单元格时,我没有触及的单元格的样式保持不变。但现在我必须处理 xlsx 文档,所以我选择了 o
我想将Excel中的数据添加到词典中。但是,当我使用.append(TOTAL_SALES)时出现错误,当然,如果我使用+=TOTAL_SALES,则没有问题,只是我获得的是总和,而不是3个单独月份的
我想将Excel中的数据添加到词典中。但是,当我使用.append(TOTAL_SALES)时出现错误,当然,如果我使用+=TOTAL_SALES,则没有问题,只是我获得的是总和,而不是3个单独月份的
如何更改excel单元格数字格式General至Text不考虑单元格中的数据? 我正在使用 openpyxl 1.8.6。 最佳答案 我知道这个问题真的很老了,但它仍然可能是相关的,因为我在谷歌搜索同
我正在其中一个 Excel 工作表中打印一些公式: wsOld.cell(row = 1, column = 1).value = "=B3=B4" 但我不能使用它的结果来实现其他一些逻辑,如: if
如何使用openpyxl将数据表添加到图例区域如下图所示: openpyxl 中有一个 openpyxl.chart.chartspace.DataTable 类,但我找不到任何示例来使用它。 最佳答
如何使用openpyxl从Excel中的命名范围读取值? 我在http://openpyxl.readthedocs.org/en/latest/_modules/openpyxl/workbook/
请有人给我一个例子,如何复制 带有样式的整个工作表(来自行和列) 到同一工作簿中的第二个工作表? (也可以在新工作簿中) 谢谢。 P.S .:我尝试进行深度复制,但是在保存更改的数据单元时失败了。 目
我有以下函数,可以使用 openpyxl 库从 excel 工作簿中读取数据: import openpyxl def read_excel(path): excel_workbook = o
我有大量 Excel 文件,我只想处理未隐藏的工作表,我想忽略所有隐藏的工作表。 目前我的 python 脚本循环遍历每张纸,无论它是否被隐藏。有没有一种简单的方法来检查工作表是否被隐藏? 我在网上看
我需要做什么: 在 Python/Pandas 中打开 Excel 电子表格 使用 [name, balance] 创建 df 例子: 姓名 平衡 琼斯事工 45,408.83 史密斯事工 38,59
我正在创建一个程序,使用 python 和 openpyxl 将所有单个引号移动到一个单引号矩阵中,但是当尝试加载模型信息时,它似乎是从第一个引号重复模型。每个报价都是一个单独的 excel 文件,这
我有一个在 openpyxl 中打开并清理它并关闭工作簿的 excel 文件,示例代码如下: #filename is the name of the excel file used wb = loa
我正在使用 openpyxl库在电子表格上执行一些 excel 剪切/粘贴操作。 假设我的操作产生了以下数据(请将其用于重现性目的): col1;col2 1;0,17153686 2;0,61532
我想写一个新的工作簿,一张纸,然后在这张纸上添加一个表格。 AFAIK 应该是可能的,这个提交应该做到 https://bitbucket.org/openpyxl/openpyxl/commits/
我是一名优秀的程序员,十分优秀!