Python processing Excel (ii): Write

Source: Internet
Author: User
Tags ticket

Code reference from Zhoujie. The function interface can refer to the blog.

The basic write function interface is simple:

Create a new Excel file

file = xlwt. Workbook ( Note that the Workbook first letter is capitalized)

Create a new sheet

Table = File.add_sheet (' Sheet_name ')

Write Data Table.write (rows, columns, value)

Table.write (0,0, ' test ')

If it is written in Chinese , it should be in the form of u ' kanji ' . Like what

Table.write (0,0, u ' kanji ')

Merge cells:

Table.write_merge (x, x + M, y, y + N, string, style)

X is the row, Y is the column, m indicates the number of spans, n is the number of spans, and string represents the cell content to write, and the style represents the cell style.

1 #Coding=utf82 " "3 Set cell style4 " "5 ImportXLWT, Xlrd6 7 defSet_style (name,height,bold=False):8style = XLWT. Xfstyle ()#Initialize Style9 TenFont = XLWT. Font ()#Create a font for a style OneFont.Name = Name#' Times New Roman ' AFont.Bold =Bold -Font.color_index = 4 -Font.height =Height the  -     #borders= XLWT. Borders () -     #borders.left= 6 -     #borders.right= 6 +     #borders.top= 6 -     #borders.bottom= 6 +  AStyle.font =Font at     #style.borders = Borders -  -     returnstyle -  -  - #Write Excel in defwrite_excel (): -f = xlwt. Workbook ()#Create a workbook to  +     " " - Create a first sheet: the Sheet1 *     " " $Sheet1 = F.add_sheet (u'Sheet1', cell_overwrite_ok=true)#Create sheetPanax NotoginsengRow0 = [u'Business', u'Status', u'Beijing', u'Shanghai', u'Guangzhou', u'Shenzhen', u'Subtotal Status', u'Total'] -Column0 = [u'Ticket', u'Ferry Tickets', u'Train Tickets', u'Car Tickets', u'other'] thestatus = [u'Booking', u'Ticket out', u'Refund', u'Subtotal Business'] +  A     #Generate first Row the      forIinchRange (0,len (row0)): +Sheet1.write (0,i,row0[i],set_style ('Times New Roman', 220, True)) -  $     #generate first and last columns (merge 4 rows) $I, J = 1, 0 -      whileI < 4*len (column0) andJ <Len (column0): -Sheet1.write_merge (I,i+3,0,0,column0[j],set_style ('Arial', 220,true))#first column theSheet1.write_merge (i,i+3,7,7)#last column "totals" -i + = 4WuyiJ + = 1 the  -Sheet1.write_merge (21,21,0,1,u'Total', Set_style ('Times New Roman', 220, True)) Wu  -     #Generate second column Abouti =0 $      whileI <Len (column0): -          forJinchRange (0,len (status)): -Sheet1.write (j+i+1,1, Status[j]) -i + = 4 A  +F.save ('Demo1.xls')#Save the file. If this is a. xlsx, it will not open.  the  - if __name__=='__main__': $     #Generate_workbook () the     #Read_excel () theWrite_excel ()

Note: If the resulting file is Demo1. xlsx , the. xls is no problem.

If you repeat the operation on a cell, an erroris raised. So when opened, add cell_overwrite_ok=true to solve

Table = File.add_sheet (' sheet name ', cell_overwrite_ok=true)

The resulting Demo1.xls effect is as follows.

Python processing Excel (ii): Write

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.