Any suggestions? If I ever the row height in openpyxl, delete_rows will appear to remove the rows, but when I save and open in Excel, the rows are empty but not all row information is removed. The scroll bar still scrolls to where the last row was prior to deleting. So “ghosts” of the empty rows remain. Sample code:
from openpyxl import Workbook
# Create a new workbook and select the active sheet
wb = Workbook()
ws = wb.active
# Write values into the cells
for row in range(1, 101):
for col in range(1, 6):
cell = ws.cell(row=row, column=col)
cell.value = f"Cell {row}-{col}"
# Set row height
for row in ws.iter_rows():
ws.row_dimensions[row[0].row].height = 14.4
# Unset row height
for row in ws.iter_rows():
ws.row_dimensions[row[0].row].height = None
# Delete the rows
ws.delete_rows(35, 100-35)
# Save the workbook
wb.save('test.xlsx')
- If I comment out both row height loops, it works correctly.
- If either loop is included, remnants of the empty rows remain.
New contributor
Officer Obie is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.