我需要包含Excel中数据的特定列中的最后一行。在openpyxl sheet.max_row或max_column中,我们获取整个工作表中的最大行数或列数。但是我想要的是特定的列。
我的场景是必须从数据库中获取一些值,并将其附加到Excel工作表中特定列的末尾。
在此屏幕截图中,如果我希望max_column在“ C”列中包含数据,则应返回10:
在上图中,如果我想要最后一个包含列“ C”的数据的单元格,则应返回10
-------------解决方案1 --------------------
import pandas as pd
# lt is the dataframe containing the data to be loaded to excel file
for index,i in enumerate(lt):
panda_xl_rd = pd.read_excel('file.xlsx',"sheet_Name") # Panda Dataframe
max = len(panda_xl_rd.iloc[:,(col-1)].dropna())+2 ''' getting the
row_num of
last record in
column
dropna removes
the Nan
values else we
will get
the entire
sheets max
column length .
+2 gets
the next column
right after the
last column to
enter data '''
cellref = sheet.cell(row = max+index, column=col)
cellref.value = i
del panda_xl_rd
------------------------解决方案2 ----------------------
------------------------解决方案3 ----------------------
也许解决方案3是更简洁的一种!
问题:我希望max_column在列“ C”中包含数据,它应该返回10:
简单计数cell.value not Empty
文档访问多个单元格
伪码
for cell in Column('C'): if not cell.value is empty: count += 1
评论:如果我们之间有一个空单元格该怎么办?
与“列范围”同步计数行,并使用一个maxRowWithData
变量。这也将在它们之间没有空单元格的情况下工作。
伪码
for row index, cell in enumerate Column('C'): if not cell.value is empty: maxRowWithData = row index
注:的细胞指数
openpyxl
是基于1!
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句