E
E
Egor Vaganov2019-05-19 15:44:24
Python
Egor Vaganov, 2019-05-19 15:44:24

How to delete first 5 rows in Excel file using Python?

It is required using Python to open an Excel file (.xls), delete the first 5 lines in it, and then save this file. Please help me with the code.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nikolay Baryshnikov, 2019-05-19
@p141592

You need to use the library to open the file and write it again. When writing, skip the first 5 lines. Just don't make a mistake with the first line. This is the title, it needs to be written down.

book = xlwt.Workbook()

# Add a sheet to the workbook
sheet1 = book.add_sheet("Sheet1")

# The data
cols = ["A", "B", "C", "D", "E"]
txt = [0,1,2,3,4]

# Loop over the rows and columns and fill in the values
for num in range(5):
      row = sheet1.row(num)
      for index, col in enumerate(cols):
          value = txt[index] + num
          row.write(index, value)

# Save the result
book.save("test.xls")

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question