简体   繁体   English

如何在python中将Excel的内容读取到对象

[英]how to read content of excel to an object in python

I have an excel file, named 'list.xlsx' that looks like this: 我有一个名为'list.xlsx'的excel文件,看起来像这样:

email username password firstname lastname jobtitle ... 电子邮件用户名密码名姓职务...

w@he.com tom 123 Tom White CEO w@he.com汤姆123汤姆·怀特(Tom White)首席执行官

and I want to use this file to fill in this web page: [signuppage] ( http://www.enterprisecloudnews.com/webinar.asp?webinar_id=1110&webinar_promo=2587 ) Here is the error message I get: 并且我想使用此文件填写此网页:[signuppage]( http://www.enterprisecloudnews.com/webinar.asp?webinar_id=1110&webinar_promo=2587 )这是我收到的错误消息:

Traceback (most recent call last): File "C:\\Users\\whoareyou\\AppData\\Local\\Programs\\Python\\Python36-32\\exceltopage.py", line 10, in formData = {'email':sheet[Bi].value, 'pin':sheet[str(Ci)].value, 'firstname':sheet[str(Di)].value, NameError: name 'Bi' is not defined 追溯(最近一次通话):文件“ C:\\ Users \\ whoareyou \\ AppData \\ Local \\ Programs \\ Python \\ Python36-32 \\ exceltopage.py”,第10行,格式为formData = {'email':sheet [Bi]。值,'pin':sheet [str(Ci)]。value,'firstname':sheet [str(Di)]。value,NameError:名称'Bi'未定义

Below is my python code, but I don't know how to read a line in my excel file to my object, so that my code can work to fill this webpage in: 以下是我的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)`

使用pandas, df=pd.read_excel("path of the file")您可以通过pandas布尔操作访问excel单元格的任何单元格

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

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