# How to clear data from Excel worksheets

**URL:** <https://discuss.python.org/t/how-to-clear-data-from-excel-worksheets/3648>\
**Category:** Python Help\
**Created:** [March 5, 2020, 1:31pm UTC](https://discuss.python.org/t/how-to-clear-data-from-excel-worksheets/3648 "2020-03-05T13:31:10Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![SachinBizboy](https://avatars.discourse-cdn.com/v4/letter/s/9fc348/32.png) [@SachinBizboy](https://discuss.python.org/u/SachinBizboy)\
**Post date:** [March 5, 2020, 1:31pm UTC](https://discuss.python.org/t/how-to-clear-data-from-excel-worksheets/3648/1 "2020-03-05T13:31:10Z")

</div>

Hi,  
I am using a loop to clear contents of a worksheet. I am not sure if this is efficient way to do. Can any please guide me with more efficient way to do it.

```python
import openpyxl
wbkName = r'\\AA\ AA \ AA \ AA \Python Chart Data.xlsx'# Please change path
wbk = openpyxl.load_workbook(wbkName)

#This loop is used to clear contents of worksheets, I am trying more efficient way for this.
for wks in wbk.worksheets:
    for row in wks['A1:J10']:
      for cell in row:
        cell.value = None #

for wks in wbk.worksheets:# This will write for all the worksheets in workbook
	for myRow in range(1, 6):# 6 will give results till row 5 not 6
		for myCol in range(1,11):# 11 will give data till column 10
			wks.cell(row=myRow, column=myCol).value = myRow 
            
wbk.save(wbkName)
wbk.close ()

```

---

<div class="post-metadata">

**Author:** ![Rupesh.Kanu](https://avatars.discourse-cdn.com/v4/letter/r/ecc23a/32.png) [@Rupesh.Kanu](https://discuss.python.org/u/Rupesh.Kanu)\
**Post date:** [July 30, 2020, 5:56pm UTC](https://discuss.python.org/t/how-to-clear-data-from-excel-worksheets/3648/2 "2020-07-30T17:56:46Z")

</div>

Please try this code with your variables and let me know if it is resolved:

```python
#This code will clear 6 columns and 100 rows so that when we enter new data or list in excel it starts from the beginning.

import openpyxl
wb=openpyxl.load_workbook('openpyxl.xlsx')
ws=wb.get_sheet_by_name('Raw Data')

ws.delete_cols(1, 6)
ws.delete_rows(1, 100)

```

---

<div class="post-metadata">

**Author:** ![SachinBizboy](https://avatars.discourse-cdn.com/v4/letter/s/9fc348/32.png) [@SachinBizboy](https://discuss.python.org/u/SachinBizboy)\
**Post date:** [August 5, 2020, 8:36am UTC](https://discuss.python.org/t/how-to-clear-data-from-excel-worksheets/3648/3 "2020-08-05T08:36:13Z")

</div>

Hi Rupesh,

Thanks for the help. Sorry for late reply. Have a nice day ahead. 🙂

---

<div class="post-metadata">

**Author:** ![FaizanScribe](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/faizanscribe/32/17680_2.png) [@FaizanScribe](https://discuss.python.org/u/FaizanScribe)\
**Post date:** [February 6, 2024, 7:30am UTC](https://discuss.python.org/t/how-to-clear-data-from-excel-worksheets/3648/4 "2024-02-06T07:30:28Z")

</div>

Hi Rupesh,  
I have been trying this on an .xlsm file, nothing happens, and no error message.  
you have any idea why nothing happens?
