首页 文章

如何在python中读取excel的内容到一个对象

提问于
浏览
1

我有一个名为'list.xlsx'的excel文件,如下所示:

email username password firstname lastname jobtitle ...

w@he.com tom 123 Tom White首席执行官

我想用这个文件来填写这个网页:[signuppage](http://www.enterprisecloudnews.com/webinar.asp?webinar_id=1110&webinar_promo=2587)这是我得到的错误信息:

回溯(最近一次调用最后一次):文件“C:\ Users \ whoareyou \ AppData \ Local \ Programs \ Python \ Python36-32 \ exceltopage.py”,第10行,在formData = {'email':sheet [Bi] .value,'pin':sheet [str(Ci)] . value,'firstname':sheet [str(Di)] . value,NameError:name'Bi'未定义

下面是我的python代码,但我不知道如何读取我的excel文件中的一行到我的对象,以便我的代码可以用来填充这个网页:

import openpyxl, pyautogui, os, time
os.chdir('C:\\users\\whoareyou')
wb = openpyxl.load_workbook('list.xlsx')
sheet = wb['Sheet1']
nameField = (168,240)
#HELP: How do I read the excel file to become the content of person?
for i in range(2,10):
    formData = {'email':sheet[Bi].value, 'pin':sheet[str(Ci)].value,
 'firstname':sheet[str(Di)].value,'lastname':sheet[str(Ei)].value,  
'job_title':sheet[str(Fi)].value,'company':sheet[str(Gi)].value,
'Address':sheet[str(Hi)].value, 'city':sheet[str(Ii)].value,
'State':sheet[str(Ji)].value,'Zip':sheet[str(Ki)].value,
'country':sheet[str(Li)].value, 'phone':sheet[str(Mi)].value,
'ba':sheet[str(Ni)].value}
pyautogui.PAUSE = 0.5
for person in formData:
    #Give the user a chance to kill the script
    print('>>>5 SECOND PAUSE TO LET USER PRESS CTRL+C')
    time.sleep(15)
#Wait until the form page has loaded.
pyautogui.typewrite('pagedown')
print('Entering %s info...' %(person['email']))
pyautogui.click(nameField[0],nameField[1])
#Fill out the email adress Field
pyautogui.typerwrite(person['email'] +'\t')
#Fill out the Username
pyautogui.typerwrite(person['username']+'\t')
#Fill out the Create a pin
pyautogui.typerwrite(person['pin']+'\t')
#Fill out the Create a pin-double
pyautogui.typerwrite(person['pin']+'\t')
#Fill out the Create a firstname, Lastname, Job Title, Company, Address,
#       City, State, ZIP,
pyautogui.typerwrite(person['firstname']+'\t')
pyautogui.typerwrite(person['lastname']+'\t')
pyautogui.typerwrite(person['job_title']+'\t')
#Select a country
if person['country'] == 'Spain':
    pyautogui.typewrite(['down','down','down','down','down','down','\t'])
elif person ['country'] == 'Germany': 
pyautogui.typewrite(['down','down','down','down','down','down',
'down','down','down','\t'])
else:pyautogui.typewrite('down','down','down','down','down','down',
'down','down','down','\t')
#Fill out phone number
pyautogui.typerwrite(person['phone']+'\t')
#Select area of business, job function
if person['ba'] == 'CEO':
        pyautogui.typewrite(['down','\t'])
if person['ba'] == 'IT staffing':
        pyautogui.typewrite(['down','down','down','down','\t'])
#Skip other
pyauto.typewrite(['down','\t'])
#Select annual revenues, employess in entire org.
pyauto.typewrite(['down','down','\t'])
#click four AGBs and I am not a robot.
time.sleep(120)`

1 回答

  • 0

    使用pandas, df=pd.read_excel("path of the file") 你可以通过pandas boolean operations访问excel单元格的任何单元格

相关问题