我正在使用openpyxl尝试从电子表格中删除行。我知道有一个专门用于删除行的函数,但是,我试图在不了解该函数的情况下解决这个问题,现在我想知道为什么我的方法不起作用。你知道吗
为了简化这个问题,我建立了一个电子表格,并在其中一些单元格中填充了字母。在本例中,第一个print(sheet.max_row)
打印“9”。将所有单元格值设置为None
后,我希望行数为0,但是第二个print语句再次打印“9”。你知道吗
是否可以通过将行中的所有单元格设置为“无”来减少行数?你知道吗
import openpyxl
from openpyxl import load_workbook
from openpyxl.utils import get_column_letter, column_index_from_string
spreadsheet = load_workbook(filename = pathToSpreadsheet) #pathToSpreadsheet represents the absolute path I had to the spreadsheet that I created.
sheet = spreadsheet.active
print(sheet.max_row) # Printed "9".
rowCount = sheet.max_row
columnCount = sheet.max_column
finalBoundary = get_column_letter(columnCount) + str(rowCount)
allCellObjects = sheet["A1":finalBoundary]
for rowOfCells in allCellObjects:
for cell in rowOfCells:
cell.value = None
print(sheet.max_row) # Also printed "9".
谢谢你的时间和努力!你知道吗
简短回答:不。 但是,可以使用单元格坐标从图纸中访问单元格并将其删除。你知道吗
更详细一点的答案是Openpyxl中的工作表将其
_cells
存储为dict
,坐标作为键。max_row
属性已定义因此,如果单元格为“无”,则键/坐标仍将占上风,例如:
_cells = {(1,1):None, (1,2):None, (5,4): None}
。max_row
仍然会给出键的最大y分量。你知道吗相关问题 更多 >
编程相关推荐