Python operation Excel table, XLWT module use

Source: Internet
Author: User

Python can manipulate the Excel module more than one, I used to use the Write module is XLWT (generally read and write modules separately) using PIP install XLWT on the line, very conventional waydirect into Code analysis, the source of this article
wk = XLWT. Workbook (encoding= "Utf-8") Sheet_test = Wk.add_sheet ("Test")

The first sentence: Create a workbook, set the encoding format as "Utf-8", the default format is ASCII, in order to facilitate the writing of Chinese, generally set to UTF-8

The second sentence: Add a sheet table to the workbook and set the table name to ' test '

Wk.save ("Xlwt_test.xls")

Save: If the table does not do anything, you can also save directly, enter the above code directly, create an empty table.

Of course, since creating a table is, of course, for writing information, what can you do before you save the table?

Let's take a look.

 for  in range (5= 256 * 20

The above code is set the column width

Col () is the column, the parameter is the specified column, the first column is starting at 0, width is the column, and the following 256*20 represents the number of characters, and 20 for 20 characters.

Font = XLWT. Font () # Font.Name = "Arial"    #字体名称 # font.bold = True  # font Bold font.height = *  # font Determines the row height, and the following number can determine the font font.col Our_index = 2style_1 = XLWT. Xfstyle () Style_1.font =font

The above code is first set up a font, font attributes have a name, whether bold, font size and color, etc.

Then add a style style style_1, set the font style to the font styles set above

Sheet_test.write (0,0, "Font color designator {}". Format (fsize), style_1)

The function of sunning writing information to a table is write ()

The first parameter is a row, starting with 0

The second argument is a column, starting with 0

The third parameter is the string object that is written

The fourth parameter can choose whether to add, is style style, you can add the previously set style, information will follow the style to write

Patterni = XLWT. Pattern () Patterni.pattern = XLWT. Pattern.SOLID_PATTERNpatterni.pattern_fore_colour = 4style_2 = XLWT. Xfstyle () Style_2.pattern = Patterni

Of course, the style has more than fonts, as well as background color and border, etc., the above code is used to set the background color, the color code can see the article to the link

Style_3 = XLWT.EASYXF ("font:name times New roman,color-index 20,bold on")

There is a quick way to set style, the above code set some font style, temporarily did not find the setting background color, later if found can update come over

Specific font color and background color code can be see the picture, this is the source Code output test table

Sheet_test.insert_bitmap ("Gakki.bmp", i,3,2,2,0.1,0.1)

Then take a look at how to insert a picture into a table (but it feels like a chicken, it's useless, why do you say that, see the code comment)

Insert_bitmap (IMG, x, y, x1, y1, scale_x=0.1, scale_y=2)

IMG Represents the address of the image to be inserted

X represents the row, and Y represents the column

X1,y1 the pixels that are offset from the original position downward to the right

Scale_x,y represents the proportion of the original width and height, the picture can be enlarged and reduced

The most important thing is, the image format must be BMP format to insert success, is not very chicken?!!

    # Merge cell tests    " "     write_merge (x, x + H, y, W + y, String, sytle)    x represents the row, Y is the column, W indicates the number of spans, H is the number of spans, string represents the cell content to write, and the style represents the cell style    Note that the x,y,w,h is calculated starting at 0.     " "     sheet_test.write_merge (0,0+3,4,4+3," merge Test ")

The following is a useful merge cell operation for Excel, which explains this usage

The above is generally written to Excel operations, remember that the information is written to the end must save the table.

Python operation Excel table, XLWT module use

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.