Ashley Ashley - 1 year ago 145
Python Question

Python: Excel to Web to PDF

I'm new to programming and am searching for the best way to pull PDFs of a series of water bills from a city website. I have been able to open the webpage and been able to open an account using an account numbers from an excel list, however, I am having trouble creating a loop to run through all accounts without rewriting code. I have some ideas, but I'm guessing that better suggestions exist. See below for the intro code:

import bs4, requests, openpyxl, os


addresses = openpyxl.load_workbook ('WaterBills.xlsx')
type (addresses)
sheet = addresses.get_sheet_by_name ('Sheet1')
cell = sheet ['A1']

from selenium import webdriver
browser = webdriver.Firefox()
elem = browser.find_element_by_css_selector('#MainContent_AcctNum')
elem.send_keys (cell.value)
elem = browser.find_element_by_css_selector('#MainContent_btnLookup')

Thanks for your assistance!

Answer Source

Couldn't find a nice way to download the PDF but here's everything but:

    import openpyxl

    from selenium import webdriver

    workbook        = openpyxl.load_workbook('WaterBills.xlsx')
    sheet           = workbook.get_sheet_by_name('Sheet1')
    column_a        = sheet.columns[0]
    account_numbers = [row.value for row in column_a if row.value]

    browser = webdriver.Firefox()

    for account_number in account_numbers:
        search_box = browser.find_element_by_id('MainContent_AcctNum')

        search_button = browser.find_element_by_id('MainContent_btnLookup')

        # TODO: download the page as a PDF