本文实例讲述了Python使用xlwt模块操作Excel的方法。分享给大家供大家参考,具体如下:
部分摘自官网文档.
该模块安装很简单
$ pip install xlwt
先来个简单的例子:
#!/usr/bin/python#coding=utf-8# ==============================================================================## Filename: demo.py# Description: excel operat# Created: Tue Apr 25 17:10:33 CST 2017# Author: Yur## ==============================================================================import xlwt# 创建一个workbook 设置编码workbook = xlwt.Workbook(encoding = 'utf-8')# 创建一个worksheetworksheet = workbook.add_sheet('My Worksheet')# 写入excel# 参数对应 行, 列, 值worksheet.write(1,0, label = 'this is test')# 保存workbook.save('Excel_test.xls')
运行后 会在当前目录生成一个Excel_test.xls
官方例子:
运行这个例子的时候 报错
自己又写了一个:
#!/usr/bin/python#coding=utf-8# ==============================================================================## Filename: style.py# Description: style# Created: Thu Apr 27 15:07:53 CST 2017# Author: Yur## ==============================================================================import xlwtworkbook = xlwt.Workbook(encoding = 'ascii')worksheet = workbook.add_sheet('My Worksheet')style = xlwt.XFStyle() # 初始化样式font = xlwt.Font() # 为样式创建字体font.name = 'Times New Roman'font.bold = True # 黑体font.underline = True # 下划线font.italic = True # 斜体字style.font = font # 设定样式worksheet.write(0, 0, 'Unformatted value') # 不带样式的写入worksheet.write(1, 0, 'Formatted value', style) # 带样式的写入workbook.save('formatting.xls') # 保存文件
效果:
设置单元格宽度:
import xlwtworkbook = xlwt.Workbook()worksheet = workbook.add_sheet('My Sheet')worksheet.write(0, 0,'My Cell Contents')# 设置单元格宽度worksheet.col(0).width = 3333workbook.save('cell_width.xls')
输入一个日期到单元格:
import xlwtimport datetimeworkbook = xlwt.Workbook()worksheet = workbook.add_sheet('My Sheet')style = xlwt.XFStyle()style.num_format_str = 'M/D/YY' # Other options: D-MMM-YY, D-MMM, MMM-YY, h:mm, h:mm:ss, h:mm, h:mm:ss, M/D/YY h:mm, mm:ss, [h]:mm:ss, mm:ss.0worksheet.write(0, 0, datetime.datetime.now(), style)workbook.save('Excel_Workbook.xls')
向单元格添加一个公式:
import xlwtworkbook = xlwt.Workbook()worksheet = workbook.add_sheet('My Sheet')worksheet.write(0, 0, 5) # Outputs 5worksheet.write(0, 1, 2) # Outputs 2worksheet.write(1, 0, xlwt.Formula('A1*B1')) # Should output "10" (A1[5] * A2[2])worksheet.write(1, 1, xlwt.Formula('SUM(A1,B1)')) # Should output "7" (A1[5] + A2[2])workbook.save('Excel_Workbook.xls')
向单元格添加一个超链接:
import xlwtworkbook = xlwt.Workbook()worksheet = workbook.add_sheet('My Sheet')worksheet.write(0, 0, xlwt.Formula('HYPERLINK("http://www.google.com";"Google")')) # Outputs the text "Google" linking to http://www.google.comworkbook.save('Excel_Workbook.xls')
合并列和行:
import xlwtworkbook = xlwt.Workbook()worksheet = workbook.add_sheet('My Sheet')worksheet.write_merge(0, 0, 0, 3, 'First Merge') # Merges row 0's columns 0 through 3.font = xlwt.Font() # Create Fontfont.bold = True # Set font to Boldstyle = xlwt.XFStyle() # Create Stylestyle.font = font # Add Bold Font to Styleworksheet.write_merge(1, 2, 0, 3, 'Second Merge', style) # Merges row 1 through 2's columns 0 through 3.workbook.save('Excel_Workbook.xls')
设置单元格内容的对其方式:
import xlwtworkbook = xlwt.Workbook()worksheet = workbook.add_sheet('My Sheet')alignment = xlwt.Alignment() # Create Alignmentalignment.horz = xlwt.Alignment.HORZ_CENTER # May be: HORZ_GENERAL, HORZ_LEFT, HORZ_CENTER, HORZ_RIGHT, HORZ_FILLED, HORZ_JUSTIFIED, HORZ_CENTER_ACROSS_SEL, HORZ_DISTRIBUTEDalignment.vert = xlwt.Alignment.VERT_CENTER # May be: VERT_TOP, VERT_CENTER, VERT_BOTTOM, VERT_JUSTIFIED, VERT_DISTRIBUTEDstyle = xlwt.XFStyle() # Create Stylestyle.alignment = alignment # Add Alignment to Styleworksheet.write(0, 0, 'Cell Contents', style)workbook.save('Excel_Workbook.xls')
为单元格议添加边框:
# Please note: While I was able to find these constants within the source code, on my system (using LibreOffice,) I was only presented with a solid line, varying from thin to thick; no dotted or dashed lines.import xlwtworkbook = xlwt.Workbook()worksheet = workbook.add_sheet('My Sheet')borders = xlwt.Borders() # Create Bordersborders.left = xlwt.Borders.DASHED DASHED虚线 NO_LINE没有 THIN实线# May be: NO_LINE, THIN, MEDIUM, DASHED, DOTTED, THICK, DOUBLE, HAIR, MEDIUM_DASHED, THIN_DASH_DOTTED, MEDIUM_DASH_DOTTED, THIN_DASH_DOT_DOTTED, MEDIUM_DASH_DOT_DOTTED, SLANTED_MEDIUM_DASH_DOTTED, or 0x00 through 0x0D.borders.right = xlwt.Borders.DASHEDborders.top = xlwt.Borders.DASHEDborders.bottom = xlwt.Borders.DASHEDborders.left_colour = 0x40borders.right_colour = 0x40borders.top_colour = 0x40borders.bottom_colour = 0x40style = xlwt.XFStyle() # Create Stylestyle.borders = borders # Add Borders to Styleworksheet.write(0, 0, 'Cell Contents', style)workbook.save('Excel_Workbook.xls')
为单元格设置背景色:
import xlwtworkbook = xlwt.Workbook()worksheet = workbook.add_sheet('My Sheet')pattern = xlwt.Pattern() # Create the Patternpattern.pattern = xlwt.Pattern.SOLID_PATTERN # May be: NO_PATTERN, SOLID_PATTERN, or 0x00 through 0x12pattern.pattern_fore_colour = 5 # May be: 8 through 63. 0 = Black, 1 = White, 2 = Red, 3 = Green, 4 = Blue, 5 = Yellow, 6 = Magenta, 7 = Cyan, 16 = Maroon, 17 = Dark Green, 18 = Dark Blue, 19 = Dark Yellow , almost brown), 20 = Dark Magenta, 21 = Teal, 22 = Light Gray, 23 = Dark Gray, the list goes on...style = xlwt.XFStyle() # Create the Patternstyle.pattern = pattern # Add Pattern to Styleworksheet.write(0, 0, 'Cell Contents', style)workbook.save('Excel_Workbook.xls')
希望本文所述对大家Python程序设计有所帮助。
新闻热点
疑难解答