簡體   English   中英

如何在python中將Excel的內容讀取到對象

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

我有一個名為'list.xlsx'的excel文件,看起來像這樣:

電子郵件用戶名密碼名姓職務...

w@he.com湯姆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]。值,'pin':sheet [str(Ci)]。value,'firstname':sheet [str(Di)]。value,NameError:名稱'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)`

使用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