Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
144 views
in Technique[技术] by (71.8m points)

How To Delete A Row In Excel Sheet Using Python?

I am working on a Selenium program using Python where I want to delete a row in the Excel sheet using Openpyxl library. The issue is I don't know how to implement the delete function in my program. Below here have 2 classes, AutoTest.py which is the testing class and NewCard.py which is the class where I implemented POM(Page Object Model). May I know how to implement the function to delete just 1 row in accordance with my program?

AutoTest.py

import unittest
import HtmlTestRunner
from selenium import webdriver
import openpyxl
import sys
sys.path.append("C:UserslukegohDesktopPython ProjectsSoftwareAutomationTesting")
from pageObjects.LoginPage import LoginPage
from pageObjects.HomePage import Homepage
from pageObjects.NewCard import NewCard

excpath = r"C:UserslukegohDesktopPython ProjectsSoftwareAutomationTestingexcel
ewABT0475EC Card Init Detailed Rpt Test 01 - v13_1.xlsx"
excsheetName = "ABT0475EC Card Initialization D"

class AutoTest(unittest.TestCase):
    baseURL = "https://10.2.5.215:33000/viewTopUpRequest"
    username = "ezltest2svc"
    password = "Password123!"
    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("Test Passed")


   # @classmethod
   # def tearDownClass(cls):
   #     cls.driver.close()
   #     print("Test Completed")


#if __name__ == '__main__':
#    unittest.main(testRunner=HtmlTestRunner.HTMLTestRunner(output='C:/Users/lukegoh/Desktop/Python Projects/SoftwareAutomationTesting/reports'))

NewCard.py

import openpyxl

class NewCard:

    jobOrder_xpath="//body/div[@id='wrapper']/div[3]/div[1]/div[1]/div[1]/div[1]/input[1]"
    excelButton_xpath="//input[@id='img']"
    topup_textbox_xpath="//input[@id='input-live']"
    calculateAmount_xpath="(//button[@type='button'])[4]"
    buttonSubmit_xpath="(//button[@type='button'])[5]"


    def __init__(self,driver):
        self.driver=driver


    def setJobOrder(self):
        self.driver.find_element_by_xpath(self.jobOrder_xpath).send_keys("AutoTest1")

    def clickExcel(self):
        self.driver.find_element_by_xpath(self.excelButton_xpath).send_keys(r"C:UserslukegohDesktopPython ProjectsSoftwareAutomationTestingexcel
ewABT0475EC Card Init Detailed Rpt Test 01 - v13_1.xlsx")

    def setTopUpAmount(self):
        self.driver.find_element_by_xpath(self.topup_textbox_xpath).send_keys("10")

    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)
question from:https://stackoverflow.com/questions/65870472/how-to-delete-a-row-in-excel-sheet-using-python

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If you check OpenPyXL docs, it has something called delete_rows()

Syntax delete_rows(idx, amount=1), it deletes from idx and goes on deleting for amount

import openpyxl

filename = "example.xlsx"
wb = openpyxl.load_workbook(filename)
sheet = wb['Sheet1']
sheet.delete_rows(row_number, 1)
wb.save(filename)

Docs for delete_rows(): https://openpyxl.readthedocs.io/en/stable/api/openpyxl.worksheet.worksheet.html#openpyxl.worksheet.worksheet.Worksheet.delete_rows


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...