Ashley Ashley - 3 months ago 33
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

os.chdir('C:\\Users\\jsmith.ABCINC\\Desktop')

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

from selenium import webdriver
browser = webdriver.Firefox()
browser.get('https://secure.phila.gov/WRB/WaterBill/Account/GetAccount.aspx')
elem = browser.find_element_by_css_selector('#MainContent_AcctNum')
elem.click()
elem.send_keys (cell.value)
elem = browser.find_element_by_css_selector('#MainContent_btnLookup')
elem.click()


Thanks for your assistance!

Answer

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()
    browser.get('https://secure.phila.gov/WRB/WaterBill/Account/GetAccount.aspx')

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

        search_button = browser.find_element_by_id('MainContent_btnLookup')
        search_button.click()

        # TODO: download the page as a PDF

        browser.back()

    browser.quit()