如何使用 openpyxl 遍历 Excel sheet 的行?

How to loop through rows of the Excel sheet using openpyxl?

我正在使用 Python、Selenium、openpyxl 在线填写表格。 为了填写表格,我从 excel (.xlsx) 上的特定单元格中获取值。 (要测试您可以创建的代码和 excel 包含 2 列的文件,在 A 列下插入一些姓名,在 B 列下插入一些年龄。

我想创建一个循环,其中代码将从 driver.get("https://www.roboform.com/filling-test-all-fields") 重新开始,再次转到我需要填写表格的页面,但这次我想采取:

然后,另一个循环从第 4 行插入数据,所以我想编程再次从 driver.get("https://www.roboform.com/filling-test-all-fields") 中读取我的代码,但这次从 A4 和 [=20 中获取值=],依此类推,直到 excel 上的行为空。

使用以下代码,我可以将数据插入到在线表单中:

from selenium import webdriver
from selenium.webdriver.chrome.webdriver import WebDriver
from selenium.common.exceptions import NoSuchElementException
import openpyxl

driver: WebDriver = 
webdriver.Chrome("/Users/HHHHH/PycharmProjects/excel/driver/chromedriver")

driver.maximize_window()

excel_document = openpyxl.load_workbook(r"/Users/XPATH OF THE EXCEL FILE YOU CREATE TO TEST THIS CODE", 
data_only=True)

sheet = excel_document["Sheet1"]



driver.get("https://www.roboform.com/filling-test-all-fields")

#Insert in the form the Name of the person

prevsymbol = sheet["A2"].value
if prevsymbol == None:
    pass
else:
    try:
        driver.find_element_by_name("02frstname").send_keys(sheet["A2"].value)
    except NoSuchElementException:
        print("A2:(name) Not Found")

#Insert in the form the Last Name of the person

prevsymbol = sheet["B2"].value
if prevsymbol == None:
    pass
else:
    try:
        driver.find_element_by_name("04lastname").send_keys(sheet["B2"].value)
    except NoSuchElementException:
        print("B2:(Lastname) Not Found")

#click Save as a draft

driver.find_element_by_xpath("//*[@value='Reset']").click()

您可以使用 max_row 属性 获取 sheet 中的行数。所以,代码变成:

from selenium import webdriver
from selenium.webdriver.chrome.webdriver import WebDriver
from selenium.common.exceptions import NoSuchElementException
import openpyxl

driver: WebDriver = 
webdriver.Chrome("/Users/HHHHH/PycharmProjects/excel/driver/chromedriver")

driver.maximize_window()

excel_document = openpyxl.load_workbook(r"/Users/HHHHH/Desktop/testtesttest1.xlsx", 
data_only=True)

sheet = excel_document["Sheet1"]

for i in range(1, sheet.max_row+1):
    driver.get("https://XXXXXXXXXX")    
    # Insert in the form the Name of the person
    cell = "A" + str(i)
    prevsymbol = sheet[cell].value
    # Note that instead of doing the work at the else clause, you can negate the term
    if prevsymbol is not None:
        try:
            # Note that we can use prevsymbol here, instead of referring to cell once again
            driver.find_element_by_id("name").send_keys(prevsymbol)
        except NoSuchElementException:
            #
            print(cell + ":(name) Not Found")

    # Insert in the form the Age of the person  
    cell = "B" + str(i)
    prevsymbol = sheet[cell].value
    if prevsymbol is not None:
        try:
            driver.find_element_by_id("age").send_keys(prevsymbol)
        except NoSuchElementException:
            print(cell + ":(Age) Not Found")

    # Click Save as a draft    
    driver.find_element_by_xpath("xpath_save_as_draft").click()

我已经创建了一个助手class请看看它是否满足你的目的。此代码是在旧版本的 openpyxl 中完成的。如果需要,请更新代码。


class OpenpyxlImport(object):
    def __init__(self, file):
        self.file = file
        if self.file.name.endswith('.xls'):
            self.wb = self.xls_to_xlsx(self.file)
        else:
            self.wb = load_workbook(self.file)
        self.sheets = self.wb.worksheets

    def to_camelcase(self, string):
        text = re.sub(r'(?!^)_([a-zA-Z])', lambda m: ' ' + m.group(1).upper(), str(string))
        return text.upper()

    def to_snake_case(self, string):
        text = re.sub(r'\s', '_', str(string))
        return text.lower()

    def xls_to_xlsx(self, content):
        xls_book = xlrd.open_workbook(file_contents=content.read())
        workbook = openpyxlWorkbook()

        for i in range(0, xls_book.nsheets):
            xls_sheet = xls_book.sheet_by_index(i)
            sheet = workbook.active if i == 0 else workbook.create_sheet()
            sheet.title = xls_sheet.name

            for row in range(0, xls_sheet.nrows):
                for col in range(0, xls_sheet.ncols):
                    sheet.cell(row=row + 1, column=col + 1).value = xls_sheet.cell_value(row, col)
        return workbook

    def tally_header(self, row, fields):
        # Strip whitespace in cell value
        for cell in row:
            cell.value = cell.value.rstrip()
        return [cell.value for cell in row] == fields

    def row_to_dict(self, row):
        dct = {}
        for cell in row:
            dct[self.to_snake_case(self.get_first_sheet()[cell.column + '1'].value)] = cell.value
        return dct

    def get_sheets(self):
        return self.sheets

    def get_first_sheet(self):
        return self.sheets[0]

    def get_sheet_rows(self):
        return tuple(self.get_first_sheet().iter_rows())

# Usage
excel = OpenpyxlImport(file)
rows = excel.get_sheet_rows()
if excel.tally_header(rows[0], self.fields):
    for row in rows[1:]:
        params = excel.row_to_dict(row)