繁体   English   中英

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

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

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

  • 从单元格A2中,我获取此人的姓名并将其插入在线表格
  • 从单元格B2中,我获取此人的 LASTNAME 并将其插入在线表格中
  • 然后我单击“重置”(这是一个示例,但在实际代码中,我将单击另存为草稿)。

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

  • 从单元格A3中,该人的姓名并将其插入在线表格
  • 从单元格B3中,该人的 LASTNAME 并将其插入在线表格
  • 然后再次点击“作为草稿发送”

然后,另一个循环从第 4 行插入数据,所以我想编程从driver.get("https://www.roboform.com/filling-test-all-fields")再次读取我的代码但是这次从A4B4中获取值,依此类推,直到 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属性获取工作表中的行数。 因此,代码变为:

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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM