XLWT setting Excel cell fonts in Python pass-through method

Source: Internet
Author: User
This article mainly introduces some basic styles of using XLWT module to set the cell in Python, such as setting the background color of the cell, the bottom border line, the font, the font color, setting the column width row height, inserting the simple picture, hoping to help everyone.

The detailed procedures are as follows:


#!/usr/bin/env python#-*-coding:utf-8-*-"#只对当前文件的中文编码有效 #Create by Zhizaiqianli 2015-12-12 Version v1.0#! /usr/bin/python# Filename:Write_excel_Format.pyimport osimport time from XLWT import *from mymodule import Cur_fi Le_dirfilename = ' Testdata2.xls ' #检测当前目录下是否有TestData2. xls file, if any, clears the previously saved file if os.path.exists (filename): Os.remove ( FileName) Print time.strftime ("%y-%m-%d", Time.localtime (Time.time ())) #打印读取到当前系统时间 wbk = Workbook (encoding= ' utf-8 ') Sheet = wbk.add_sheet (' New sheet 1 ', cell_overwrite_ok=true) #第二参数用于确认同一个cell单元是否可以重设值. style = Xfstyle () #赋值style为XFStyle (), initialize style for I in range (0X00,0XFF): # Set cell background color pattern = pattern () # Create a Mode Pattern.pattern = Pattern.solid_pattern # set its mode to real pattern.pattern_fore_colour = i # set cell background Color 0 = Black, 1 = white, 2 = Red, 3 = Green, 4 = Blue, 5 = Yellow, 6 = Magenta, the list goes on ... style.pattern = pattern # Imports A well-assigned pattern parameter    Style Line_data = (U ' Test table ') #创建一个Line_data列表 and assigns its value as a test table to utf-8 the Chinese before adding u  Sheet.write_merge (i, I, 0, 2, line_data, Style) #以合并单元格形式写入数据, to write data in column 1/2/3 merge German cells for I in range (0X00,0XFF): # Set cells Inner font style FNT = Font () # Create a text format that includes font, font size, and color style attributes Fnt.name = u ' Microsoft Ya Black ' # set its font for Microsoft Jas Black fnt.colour_index = i # set its font Color Fnt.bold = True Style.font = fnt #将赋值好的模式参数导入Style sheet.write_merge (i,i,3,5,line_data,style) #以合并单元格       Writes the data, writes the data to the 4/5/6 column merged with the German cell for I in range (0, 0x53): # Sets the cell below the border line style borders = Borders () Borders.left = i Borders.right = i borders.top = i borders.bottom = i style.borders = Borders #将赋值好的模式参数导入Style S Heet.write_merge (I,i,6,8,line_data,style) #以合并单元格形式写入数据, the data is written in column 4/5/6 merge German cell for I in range (6, 80): # Set cell The following wide style sh Eet.write (0,i,line_data,style) Sheet.col (i). width = 0x0d00 + i*50path_py = "\\images\python.bmp" # Read the Insert picture with the. Py run-time path, images and. py in the same directory Path_exe = "\\images\python.bmp" # Read the Insert picture to the. exe run-time path, the. exe can be moved to any other directory, but images and. exe are in the same directory as Path = Cur_file_dir (Path_py,path_exe) #获取文件的Relative path filename = path #检测当前目录下是否有python. bmp picture, if os.path.exists (filename): print u ' python.bmp picture exists ' else:print u ' python.b     MP picture does not exist ' Sheet.insert_bitmap (path, 2, 9) #插入一个图片 wbk.save (' Testdata2.xls ') #保存TestData2. xls file, save to script or EXE file under Run directory Raw_input ("Enter enter key to exit ...") #插入一个输入命令 to make it easy to run EXE without printing information


When you run write_excel_format.py with the cmd command, the results and the resulting table are as follows:

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.