Import an Excel file with Python+selenium

Source: Internet
Author: User

Connect to MySQL

#encoding =utf-8import pymysqlimport timeclass connmysql (object):    def __init__ (self):        Self.conn = Pymysql.connect (host= ' 192.168.2.53 ', user= ' tester ', passwd= ' tester890__ ', db= ' ips_sqrz ', charset= ' UTF8 ')         Self.cur = Self.conn.cursor ()            def selectsql (self):        selectsql = ("Select Secondprojectno from Ips_smallproject_ Info ")      #??????????        Self.cur.execute (selectsql)              self.results = Self.cur.fetchall ()         def getTime (self):        self.temp = Int ( Time.strftime ('%y%m%d%h%m%s ', Time.localtime (Time.time ()))            def closesql (self):            self.cur.close ()        

Modify Excel

#!/usr/bin/env python#-*-coding:gbk-*-import sysreload (SYS) sys.setdefaultencoding (' GBK ') import xlrdfrom xlrd Import                Open_workbookfrom xlutils.copy Import copyfrom connmysql import Connmysqlclass Opexcel (object): Def __init__ (self): File = Xlrd.open_workbook ("E:\WorkSpace\python34Demo\data\\ waybill import template. xlsx") sheet = File.sheet_by_index (        0) self.sheets = copy (file) self.table = Self.sheets.get_sheet (0) Self.conn = Connmysql () SELF.CONN.SELECTSQL () def rexcel (self): print "Generate waybill template, please wait ...             "row = 1 col = 0 for each in self.conn.results:if row = = 10:pass                                      Else:self.conn.getTime () self.table.write (row, col, each) Self.table.write (Row, col+1, Self.conn.temp+row) self.sheets.save ("E:\WorkSpace\python34Dem o\data\\ waybill import template. xlsx ") Row + = 1 self.coNn.closesql () 

Implementation Import

#!/usr/bin/env python#-*-coding:gbk-*-import sysreload (SYS) sys.setdefaultencoding (' GBK ') from selenium import Webdriverimport time,sysfrom opexcel Import opexcelclass Importorder (object): Def __init__ (self): Driver = webdr Iver.        Firefox () driver.get ("http://sqrz55.ips.cn") Driver.maximize_window () Time.sleep () DRIVER.FIND_ELEMENT_BY_ID ("username"). Send_keys ("Sqrz_admin") driver.find_element_by_id ("password"). Send_keys (" Admin123 ") driver.find_element_by_class_name (" Login_btn "). Click () time.sleep (2) if DRI Ver.title = = U ' Home-G7 ': print ' login successful ' driver.get ("Http://sqrz55.ips.cn/ordercenter/impor T.html ") time.sleep (1) #直接为导入控件赋文件的绝对路径 Driver.find_element_by_xpath ('//*[@id =" Imp            Ortfile "]). Send_keys (U "E:\WorkSpace\python34Demo\data\\ waybill import template. xlsx") driver.find_element_by_id ("Btnsubmit"). Cl  Ick ()                     Time.sleep (2) driver.find_element_by_class_name ("Ui_state_highlight"). Click () succeed = Driver.find_element_by_xpath ("//span[@id = ' success_count ']"). Text #得到成功导入和失败导入的数目 Erro                r = Driver.find_element_by_xpath ("//span[@id = ' error_count ']"). Text if succeed >= 0: print ' successfully imported ' +succeed+ ' data ' print ' failed ' +error+ ' data ' else:print ' login failed, please re-login ' Driver.close () if __name__ = = ' __main__ ': ex = Opexcel () ex.rexcel () io = Impo Rtorder ()

  

Import an Excel file with Python+selenium

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.