Python Excel Read-write

Source: Internet
Author: User

1 #Coding=utf-82 3 Print "----------------Split Line xlrd--------------------"4 Importxlrd5 #Open a Wordbook6Book = Xlrd.open_workbook ("Excel_1.xls")7 8worksheets = Book.sheet_names ()#Uoqu all sheet names9 #print ' workshets: ', worksheetsTen #sheet = book.sheets () #获得全部sheet One #For item in sheet: A #Print Item.name -  -Sheet = book.sheet_by_index (0)#switch sheet by subscript the #sheet = book.sheet_by_name (' wsf ') #通过sheet的名称切换 -  -rows = Sheet.nrows#Number of rows -cols = Sheet.ncols#Number of columns +  -R_data = Sheet.row (1)#gets the specified row data and returns the list + #print r_data #[text:u ' LAD ', text:u ' v100r001c01b001 ', text:u ' Sdfa v100r001c01b001 ', Empty:u '] AC_data = Sheet.col (0)#gets the specified column data and returns the list at #print C_data #[text:u ' product name ', Text:u ' LAD ', text:u ' DSF ', text:u ' ASD ', text:u ' EFSW ' -Ce_data = Sheet.cell (.) value#gets the data for the specified cell - #Print Ce_data -  - #get all row data in sheet -  forRowinchxrange (rows): inR_data =sheet.row_values (Row) -     PrintR_data to #get all column data in the sheet +  forColinchxrange (cols): -C_data =sheet.col_values (COL) the     PrintC_data * #get data for all cells in sheet $  forRowinchxrange (rows):Panax Notoginseng      forColinchxrange (cols): -Ce_data =sheet.cell_value (Row, col) the         Print "Cell:", Ce_data +  A Print "----------------Split Line XLWT--------------------" the  + ImportXLWT - " "XLWT cannot manipulate existing Excel, new Excel writes data" " $ #Create a Workbook object $Workbook =XLWT. Workbook () - #Create sheet object, new sheet -Sheet1 = Workbook.add_sheet ('XLWT', cell_overwrite_ok=True) theSheet2 = Workbook.add_sheet ('xled', cell_overwrite_ok=True) - Wuyi #---Set the Excel style--- the #Initialize Style -style =XLWT. Xfstyle () Wu #Create a font style -Font =XLWT. Font () AboutFont.Name ='Times New Roman' $Font.Bold = True#Bold - #Set Font -Style.font =Font - #writing data using styles A #sheet.write (0, 1, "xxxxx", Style) +  the #writing data to sheet -Sheet1.write (0, 0,'Nihao XLWT', Style) $Sheet1.write (0, 1,'Nimei') theSheet2.write (0, 0,'Nihao xlrd', Style) theSheet2.write (0, 1,'Nimei') the #Save the Excel file with the same name as the direct overwrite theWorkbook.save ('Xlwt.xls') - Print 'The Excel Save success' in  the Print "----------------Split Line xlutils--------------------" the  About  fromXlutilsImportCopy the " "xlutils writing data to an Excel file, used in conjunction with XLRD" " the #Open Excel File theRB = Xlrd.open_workbook ("Xlwt.xls") +WB = Copy.copy (RB)#Copy copies to write data - #get Sheet object, cannot get sheet by RB, Xlrd no Write () method theWS =wb.get_sheet (0)BayiWs.write (0, 0,'666666666') the Print "Write Success" the #must be saved, saved as a file of the same name, unmodified part reserved -Wb.save ('Xlwt.xls') -  the Print "----------------Split Line Pyexcelerator read--------------------" the  the ImportPyexcelerator as Pyexcel the " "read the Excel file data, parse the Excel file, return the entire Excel data, return the list" " - ##parse_xls返回一个列表, each item is a sheet page of data.  the #Each item is a two-tuple (table name, cell data). Where the cell data is a dictionary, the #The key value is the index of the cell (I,J). If a cell has no data, this value does not exist theSheets = Pyexcel.parse_xls ('Xlwt.xls')94 Printsheets, type (sheets) the  the Print "----------------Split Line Pyexcelerator write--------------------" the 98 " "Pyexcelerator Write, similar to XLWT, is a new Excel to write data" " AboutWB =Pyexcel.workbook () -WS = Wb.add_sheet (U'first Page')101 #Set Style102style =Pyexcel.xfstyle ()103Font =Pyexcel.font ()104Font.Name ='Times New Roamn' theFont.Bold =True106Style.font =Font107 #writing data, using styles108Ws.write (0, 0, u' Hello', Style)109 Print "Write Success" theWb.save ('Pyexcel.xls')

Python Excel Read-write

Related Article

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.