本文主要为大家详细介绍了Python中使用xlwt模块设置单元格的一些基本样式,如设置单元格的背景颜色,下框线,字体,字体的颜色,设置列宽行高,插入简单的图片,希望能帮助到大家。
详细程序如下:
#!/usr/bin/env python # -*- coding: utf-8 -*-” #只对当前文件的中文编码有效 #Create by zhizaiqianli 2015-12-12 Version V1.0 #!/usr/bin/python # Filename : Write_excel_Format.py import os import time from xlwt import * from Mymodule import cur_file_dir filename = 'TestData2.xls' #检测当前目录下是否有TestData2.xls文件,如果有则清除以前保存文件 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(),初始化样式 for i in range(0x00,0xff): # 设置单元格背景颜色 pattern = Pattern() # 创建一个模式 pattern.pattern = Pattern.SOLID_PATTERN # 设置其模式为实型 pattern.pattern_fore_colour = i # 设置单元格背景颜色 0 = Black, 1 = White, 2 = Red, 3 = Green, 4 = Blue, 5 = Yellow, 6 = Magenta, the list goes on... style.pattern = pattern # 将赋值好的模式参数导入Style Line_data = (u'测试表') #创建一个Line_data列表,并将其值赋为测试表,以utf-8编码时中文前加u sheet.write_merge(i, i, 0, 2, Line_data, style) #以合并单元格形式写入数据,即将数据写入以第1/2/3列合并德单元格内 for i in range(0x00,0xff): # 设置单元格内字体样式 fnt = Font() # 创建一个文本格式,包括字体、字号和颜色样式特性 fnt.name = u'微软雅黑' # 设置其字体为微软雅黑 fnt.colour_index = i # 设置其字体颜色 fnt.bold = True style.font = fnt #将赋值好的模式参数导入Style sheet.write_merge(i,i,3,5,Line_data,style) #以合并单元格形式写入数据,即将数据写入以第4/5/6列合并德单元格内 for i in range(0, 0x53): # 设置单元格下框线样式 borders = Borders() borders.left = i borders.right = i borders.top = i borders.bottom = i style.borders = borders #将赋值好的模式参数导入Style sheet.write_merge(i,i,6,8,Line_data,style) #以合并单元格形式写入数据,即将数据写入以第4/5/6列合并德单元格内 for i in range(6, 80): # 设置单元格下列宽样式 sheet.write(0,i,Line_data,style) sheet.col(i).width = 0x0d00 + i*50 path_py = "\\images\python.bmp" #读取插入图片以.py运行时路径,images和.py在同一目录下 path_exe = "\\images\python.bmp" #读取插入图片以.exe运行时路径,.exe可以移到其他任意目录下运行但images和.exe在同一目录下 path = cur_file_dir(path_py,path_exe) #获取文件的相对路径 filename = path #检测当前目录下是否有python.bmp图片, if os.path.exists(filename): print u'python.bmp图片存在' else: print u'python.bmp图片不存在' sheet.insert_bitmap(path, 2, 9) #插入一个图片 wbk.save('TestData2.xls') #保存TestData2.xls文件,保存到脚本或exe文件运行的目录下 raw_input("Enter enter key to exit...") #插入一个输入命令,方便运行exe时一闪而过不到打印信息
以cmd命令运行 Write_excel_Format.py时结果和生成表格如下所示:
相关推荐:
python使用xlrd与xlwt对excel的读写和格式设定
The above is the detailed content of How to set excel cell font and format using xlwt in Python. For more information, please follow other related articles on the PHP Chinese website!

Is it enough to learn Python for two hours a day? It depends on your goals and learning methods. 1) Develop a clear learning plan, 2) Select appropriate learning resources and methods, 3) Practice and review and consolidate hands-on practice and review and consolidate, and you can gradually master the basic knowledge and advanced functions of Python during this period.

Key applications of Python in web development include the use of Django and Flask frameworks, API development, data analysis and visualization, machine learning and AI, and performance optimization. 1. Django and Flask framework: Django is suitable for rapid development of complex applications, and Flask is suitable for small or highly customized projects. 2. API development: Use Flask or DjangoRESTFramework to build RESTfulAPI. 3. Data analysis and visualization: Use Python to process data and display it through the web interface. 4. Machine Learning and AI: Python is used to build intelligent web applications. 5. Performance optimization: optimized through asynchronous programming, caching and code

Python is better than C in development efficiency, but C is higher in execution performance. 1. Python's concise syntax and rich libraries improve development efficiency. 2.C's compilation-type characteristics and hardware control improve execution performance. When making a choice, you need to weigh the development speed and execution efficiency based on project needs.

Python's real-world applications include data analytics, web development, artificial intelligence and automation. 1) In data analysis, Python uses Pandas and Matplotlib to process and visualize data. 2) In web development, Django and Flask frameworks simplify the creation of web applications. 3) In the field of artificial intelligence, TensorFlow and PyTorch are used to build and train models. 4) In terms of automation, Python scripts can be used for tasks such as copying files.

Python is widely used in data science, web development and automation scripting fields. 1) In data science, Python simplifies data processing and analysis through libraries such as NumPy and Pandas. 2) In web development, the Django and Flask frameworks enable developers to quickly build applications. 3) In automated scripts, Python's simplicity and standard library make it ideal.

Python's flexibility is reflected in multi-paradigm support and dynamic type systems, while ease of use comes from a simple syntax and rich standard library. 1. Flexibility: Supports object-oriented, functional and procedural programming, and dynamic type systems improve development efficiency. 2. Ease of use: The grammar is close to natural language, the standard library covers a wide range of functions, and simplifies the development process.

Python is highly favored for its simplicity and power, suitable for all needs from beginners to advanced developers. Its versatility is reflected in: 1) Easy to learn and use, simple syntax; 2) Rich libraries and frameworks, such as NumPy, Pandas, etc.; 3) Cross-platform support, which can be run on a variety of operating systems; 4) Suitable for scripting and automation tasks to improve work efficiency.

Yes, learn Python in two hours a day. 1. Develop a reasonable study plan, 2. Select the right learning resources, 3. Consolidate the knowledge learned through practice. These steps can help you master Python in a short time.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use