Python xls document Read/write

Source: Internet
Author: User

1. Module installation

2.python Code

1 Importxlrd2 ImportXLWT3 Importdatetime4 5 defSet_style (name,height,format,bold=False):6style =XLWT. Xfstyle ()7     ifFormat.strip ()! ="':8Style.num_format_str =format9Font =XLWT. Font ()TenFont.name=name OneFont.bold=Bold AFont.color_index=4 -font.height=Height -Alignment =XLWT. Alignment () the     #horz_general, Horz_left, Horz_center, Horz_right, horz_filled, horz_justified, Horz_center_across_sel, HORZ_ Distributed -Alignment.horz =XLWT. Alignment.horz_center -     #vert_top, Vert_center, Vert_bottom, vert_justified, vert_distributed -Alignment.vert =XLWT. Alignment.vert_center +Style.alignment =Alignment -Style.font=Font +     returnstyle A      at defSet_colstyle (sheet,cindex): -Col=Sheet.col (CIndex) -Col.width =256*20 -     #col.height =100 -      - defWritexls (path): inWB =XLWT. Workbook () -Sheet = Wb.add_sheet ('Test', cell_overwrite_ok=True) toSet_colstyle (sheet,3) +     #title -heads=['name','disciplines','score','Date'] the      forHinchRange (0,len (heads)): *Sheet.write (0,h,heads[h],set_style ('Arial', 300,"', True)) $ Panax Notoginseng      -     #Data the      +Sheet.write_merge (1,2,0,0,'Zhang San', Set_style ('Arial', 300,"', False)) ASheet.write (1, 1,'language', Set_style ('Arial', 240,"', False)) theSheet.write (1,2,85,set_style ('Arial', 240,"', False)) +Sheet.write (1,3,datetime.date.today (), Set_style ('Arial', 240,'YYYY/MM/DD', False)) -Sheet.write (2,1,'Mathematics', Set_style ('Arial', 240,"', False)) $Sheet.write (2,2,85,set_style ('Arial', 240,"', False)) $Sheet.write (2,3,datetime.date.today (), Set_style ('Arial', 240,'YYYY/MM/DD', False)) -  -Sheet.write_merge (3,4,0,0,'John Doe', Set_style ('Arial', 300,"', False)) theSheet.write (3,1,'language', Set_style ('Arial', 240,"', False)) -Sheet.write (3,2,95,set_style ('Arial', 240,"', False))WuyiSheet.write (3,3,datetime.date.today (), Set_style ('Arial', 240,'YYYY/MM/DD', False)) theSheet.write (4,1,'Mathematics', Set_style ('Arial', 240,"', False)) -Sheet.write (4,2,95,set_style ('Arial', 240,"', False)) WuSheet.write (4,3,datetime.date.today (), Set_style ('Arial', 240,'YYYY/MM/DD', False)) -      About  $ wb.save (path) -  - defIsmerge (sheet,merge,r,c): -     #Merge=sheet.merged_cells A      forMinchMerge: +        ifR>=M[0] andR<M[1] andC==m[2]: theR=M[0] -C==m[2] $             Break the          the     returnR,c the  the defReadxls (path): -Wb=xlrd.open_workbook (path,formatting_info=True) in     #sheetname=wb.sheet_names () [0] thesheet=wb.sheet_by_index (0) theRows=sheet.nrows Aboutcols=Sheet.ncols theMerge=Sheet.merged_cells the     #The meaning of these four parameters returned by Merged_cells is: (Row,row_range,col,col_range), the     #where [Row,row_range] includes row, not including Row_range +      -     Print('--------------------------------------------------------------------') the      forRinchRange (0,rows):BayiListstr = [] the          the          forCinchRange (0,cols): -merg=Ismerge (sheet,merge,r,c) -             if(Sheet.cell (Merg[0],merg[1]). ctype==3): theData_value=xlrd.xldate_as_tuple (Sheet.cell_value (merg[0],merg[1]), Wb.datemode) the                 #Print (Datetime.date (*data_value[:3]). Strftime ('%y/%m/%d ')) theListstr.append (Datetime.date (*data_value[:3]). Strftime ('%y/%m/%d')) the             Else: -                 #Print (Sheet.cell_value (merg[0],merg[1) ) theListstr.append (Sheet.cell_value (merg[0],merg[1])) the             #Print (Sheet.cell (merg[0],merg[1]). Value) the             #Print (Sheet.cell (r,c). CType)94  the         Print('|\t'. Join (STR (s) forSinchListstrifS not inch[None])) the         Print('--------------------------------------------------------------------') the     98 if __name__=='__main__': About  -     #Writexls (' h:\ test. xls ')101Readxls ('h:\ test. xls')102     103     104  the     106     107     

3. Effect display

Python xls document 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.