我似乎无法解决这个问题:
我想访问给定列中的最后一个单元格,以便为其添加值。但我的代码不起作用:
from tkinter import *
import datetime
import openpyxl
import os
wb = openpyxl.load_workbook("TimeSheet.xlsx", use_iterators=True)
ws1 = wb.get_sheet_by_name("TimeSheet")
master = Tk()
firstDate = Entry(master)
firstDate.pack()
firstDate.focus_set()
def date_receiving():
totalRows = 0
receivingDate = firstDate.get()
for rowNum in ws1.iter_rows("B6:B"+str(ws1.max_row)):
for cell in rowNum:
print(cell)
cellule = ws1.cell(row=rowNum, column=2).value
if cellule != "":
totalRows += 1
print(totalRows)
else:
break
我尝试了一些事情,却无法找到最后一个空单元格。请帮忙!
谢谢,
答案 0 :(得分:1)
以下是一些代码,可以将最后一个空单元格的编号作为maxBlank
。让我知道它对你有用。
from Tkinter import *
import datetime
import openpyxl
import os
wb = openpyxl.load_workbook("TimeSheet.xlsx", use_iterators=True)
ws1 = wb.get_sheet_by_name("TimeSheet")
master = Tk()
firstDate = Entry(master)
firstDate.pack()
firstDate.focus_set()
def date_receiving():
maxBlank = -1
for rowNum in ws1.iter_rows("B6:B"+str(ws1.max_row)):
for cell in rowNum:
if cell.internal_value == None:
if cell.row > maxBlank:
maxBlank = cell.row
print maxBlank