Tag: Ack write inherits use source Ges path Div site
1. Module invocation
The same directory down, directly from XX import xx on the line
Different directory down, preferably import sys;sys.path.append ()
When other modules are called, Python is looked for in the project's current path, Python path, and the Python installation directory.
2. Exceptions
All exceptions inherit from Baseexception,baseexception, exception can accept all exception types
Try
Except
else: #没有异常时执行
Finally: Any exception will be executed
Operations for Excel
Premise, install three packages, xlrd, XLWT, xlutils
1 #Coding=utf-82 3 Importxlrd4 #1. Open Excel path with double slash5Wb=xlrd.open_workbook ("D:\\test\\testweek3.xls")6 " "7 #2. Get sheet page s need to capitalize8 sh=wb.sheet_by_name ("Sheet1")9 #3. Gets the value of the cell cell_value (i,j) line i+1, section j+1Ten User=sh.cell_value (1,0) One Print User A - #练习1: Reads the second sheet page, gets the third row, the value of the third column - sh2=wb.sheet_by_name (' test2 ') the Age=sh2.cell_value (2,2) #整数默认输出类型为float, need to be converted to integral type - print int (age) - - #练习2: Reads all user information from the first sheet page--loop + sh3=wb.sheet_by_name (' Sheet1 ') - for I in range (1,5): #从excel第二行开始读取, starting with the number 1 + User=sh3.cell_value (i,0) A Print User at " " - #Exercise 3: First sheet, output user name and password, password conversion rule (if empty, direct output password, not empty then convert) -Sh4=wb.sheet_by_name ('Sheet1') - #number of rows R_num -r_num=sh4.nrows - forIinchRange (1, R_num): inUser=Sh4.cell_value (i,0) -Pwd=sh4.cell_value (i,1) to ifpwd=="": + Printuser,pwd - Else: the PrintUser,int (PWD)
1 #Coding=utf-82 " "3 write to Excel4 " "5 Importxlrd6 fromXlutils.copyImportCopy7 #Copy the C:\Python27\xlutils-1.7.1\xlutils to the C:\Python27\Lib\site-packages8 #1. Open Excel9Wb=xlrd.open_workbook ("D:\\test\\testweek3.xls")Ten #2. Copy the original Excel into a new Excel Onenew_wb=copy (WB) A #3. Get the sheet page and write the value Get_sheet (i) i+1 sheet page -Sh=new_wb.get_sheet (1) -Sh.write (3,4,'51testing') the #4. Save -New_wb.save ("D:\\test\\testweek3.xls")
1 #Coding=utf-82 Importxlrd3 4Path ='D:\\documents\\personal\\appium\\documents\\three-day'5EXE = Xlrd.open_workbook (path +'\\test.xls')6SH =exe.sheet_by_index (0)7num =sh.nrows8i = 19 while(i<num):Tenuser =Sh.cell_value (i,0) OnePWD = Sh.cell_value (i,1) Ai = i + 1 - ifPWD = ="': - Printuser,pwd the Else: - PrintUser, int (pwd)
1 #Coding=utf-82 Importxlrd3 fromXlutils.copyImportCopy4Path ='D:\\documents\\personal\\appium\\documents\\three-day\\source'5EXE = Xlrd.open_workbook (path +'\\test.xls')6Exe_new =copy (EXE)7SH =exe_new.get_sheet (0)8 #after copy, can only edit save, cannot get print sh.cell_value (0,0)9Sh.write (0,5,'Testing')TenExe_new.save (path+'\\test1.xls') One AExe1 = Xlrd.open_workbook (path+'\\test1.xls') -SH =exe1.sheet_by_index (0) - PrintSh.cell_value (0,5)
Python Basic &excel operation