title: How To Delete A Row In Excel Sheet Using Python?
body: <p>I am working on a <code>Selenium</code> program using <code>Python</code> where I want to delete a row in the <code>Excel</code> sheet using <code>Openpyxl</code> library. The issue is I don't know how to implement the delete function in my program. Below here have 2 classes, <code>AutoTest.py</code> which is the testing class and <code>NewCard.py</code> which is the class where I implemented <code>POM(Page Object Model)</code>. May I know how to implement the function to delete just 1 row in accordance with my program?</p>
  <p><strong>AutoTest.py</strong></p>
  <pre><code>import unittest
  import HtmlTestRunner
  from selenium import webdriver
  import openpyxl
  import sys
  sys.path.append(&quot;C:\Users\lukegoh\Desktop\Python Projects\SoftwareAutomationTesting&quot;)
  from pageObjects.LoginPage import LoginPage
  from pageObjects.HomePage import Homepage
  from pageObjects.NewCard import NewCard
  
  excpath = r&quot;C:\Users\lukegoh\Desktop\Python Projects\SoftwareAutomationTesting\excel\new\ABT0475EC Card Init Detailed Rpt Test 01 - v13_1.xlsx&quot;
  excsheetName = &quot;ABT0475EC Card Initialization D&quot;
  
  class AutoTest(unittest.TestCase):
      baseURL = &quot;https://10.2.5.215:33000/viewTopUpRequest&quot;
      username = &quot;ezltest2svc&quot;
      password = &quot;Password123!&quot;
      driver = webdriver.Chrome()
  
      @classmethod
      def setUpClass(cls):
          cls.driver.get(cls.baseURL)
          cls.driver.maximize_window()
          cls.driver.implicitly_wait(10)
  
  
      def test_1(self):  #This is scenario 1- to create request for new card
          lp = LoginPage(self.driver)
          hp = Homepage(self.driver)
          np = NewCard(self.driver)
          lp.setUsername(self.username)
          lp.setPassword(self.password)
          lp.clickLogin()
          hp.clickutil()
          hp.clickreqNewCard()
          np.setJobOrder()
          np.clickExcel()
          np.setTopUpAmount()
          np.calculateAmount()
          if not np.clickSubmit():
              np.deleteRow(excpath,excsheetName,3)
          else:
              print(&quot;Test Passed&quot;)
  
  
     # @classmethod
     # def tearDownClass(cls):
     #     cls.driver.close()
     #     print(&quot;Test Completed&quot;)
  
  
  #if __name__ == '__main__':
  #    unittest.main(testRunner=HtmlTestRunner.HTMLTestRunner(output='C:/Users/lukegoh/Desktop/Python Projects/SoftwareAutomationTesting/reports'))
  </code></pre>
  <p><strong>NewCard.py</strong></p>
  <pre><code>import openpyxl
  
  class NewCard:
  
      jobOrder_xpath=&quot;//body/div[@id='wrapper']/div[3]/div[1]/div[1]/div[1]/div[1]/input[1]&quot;
      excelButton_xpath=&quot;//input[@id='img']&quot;
      topup_textbox_xpath=&quot;//input[@id='input-live']&quot;
      calculateAmount_xpath=&quot;(//button[@type='button'])[4]&quot;
      buttonSubmit_xpath=&quot;(//button[@type='button'])[5]&quot;
  
  
      def __init__(self,driver):
          self.driver=driver
  
  
      def setJobOrder(self):
          self.driver.find_element_by_xpath(self.jobOrder_xpath).send_keys(&quot;AutoTest1&quot;)
  
      def clickExcel(self):
          self.driver.find_element_by_xpath(self.excelButton_xpath).send_keys(r&quot;C:\Users\lukegoh\Desktop\Python Projects\SoftwareAutomationTesting\excel\new\ABT0475EC Card Init Detailed Rpt Test 01 - v13_1.xlsx&quot;)
  
      def setTopUpAmount(self):
          self.driver.find_element_by_xpath(self.topup_textbox_xpath).send_keys(&quot;10&quot;)
  
      def calculateAmount(self):
          self.driver.find_element_by_xpath(self.calculateAmount_xpath).click()
  
      def clickSubmit(self):
          self.driver.find_element_by_xpath(self.buttonSubmit_xpath).click()
  
      def deleteRow(file, sheetName, rownum):
          workbook = openpyxl.load_workbook(file)
          sheet = workbook.get_sheet_by_name(sheetName)
          sheet.cell(row=rownum).
          workbook.save(file)
  </code></pre>