Python操作excel

一、基础

1、基本操作

# 安装 xlwt模块,用于写入excel数据    
pip install xlwt 

# 导入xlwt
import xlwt

# 新建一个excel文件
file = xlwt.Workbook()
# 新建一个sheet
table = file.add_sheet('sheet name')
# 写入数据table.write(行,列,value)
table.write(0,0,'test')
# 如果对一个单元格重复操作,会引发
returns error:
       # Exception: Attempt to overwrite cell:
     # sheetname=u'sheet 1' rowx=0 colx=0
# 所以在打开时加cell_overwrite_ok=True 解决
table = file.add_sheet('sheet name',cell_overwrite_ok=True )

# 保存文件
file.save('demo.xls')


# 另外,使用style
# 初始化样式
style = xlwt.XFStyle()
#为样式创建字体
font = xlwt.Font() 
font.name = 'Times New Roman'
font.bold = True
#为样式设置字体
style.font = font
# 使用样式
table.write(0, 0, 'some bold Times text', style)



xlwt 允许单元格或者整行地设置格式。还可以添加链接以及公式。可以阅读源代码,那里有例子
dates.py, 展示如何设置不同的数据格式
hyperlinks.py, 展示如何创建超链接 (hint: you need to use a formula)
merged.py, 展示如何合并格子
row_styles.py, 展示如何应用Style到整行格子中.

 

2、操作例子

# 例子1 创建一个简单的excel表
file=xlwt.Workbook()
table = file.add_sheet(u'xx巡检手册',cell_overwrite_ok=True)
table.write(0,0,u'数据')
file.save(u'巡检手册.xls')
创建一个简单的excel
# 例子2  创建一个简单的excel表,2个sheet表
# 创建一个excel表
workbook = xlwt.Workbook(encoding='utf-8')
# 创建一个sheet表
worksheet = workbook.add_sheet('我的sheet')
worksheet2 = workbook.add_sheet('我的sheet2')
# 向表中第一行第二列插入一个数据
worksheet.write(0,1,label='这是一个数据')
worksheet2.write(0,1,label='这是一个数据')
# 保存excel表
workbook.save(u'xxx巡检手册.xls')
创建一个简单的excel表,2个sheet表
# 例子3,加点样式吧
file = xlwt.Workbook(encoding='utf-8')
table = file.add_sheet('新兴业务巡检手册')
# 初始化样式
style = xlwt.XFStyle()
# 为样式创建字体
font = xlwt.Font()
font.name =  'Times New Roman'
# 黑体
font.bold = True
# 下划线
font.underline = True
# 斜体字
font.italic = True
# 设定样式
style.font = font
# 不带样式的写入数据
table.write(0,0,'写入无样式数据啦')
# 携带样式写入数据
table.write(1,0,'写入样式数据啦',style)
file.save(u'新型业务巡检手册.xls')
写点样式吧
# 例子4 设置单元格宽度
workbook = xlwt.Workbook()
worksheet = workbook.add_sheet('My Sheet')
worksheet.write(0, 0,'My Cell Contents')
# 设置单元格宽度
worksheet.col(0).width = 3333
workbook.save('cell_width.xls')
设置单元格宽度
import xlwt
import datetime
workbook = 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.0
worksheet.write(0, 0, datetime.datetime.now(), style)
workbook.save('Excel_Workbook.xls')
输入一个日期到单元格
import xlwt
workbook = xlwt.Workbook()
worksheet = workbook.add_sheet('My Sheet')
worksheet.write(0, 0, 5) # Outputs 5
worksheet.write(0, 1, 2) # Outputs 2
worksheet.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 xlwt
workbook = 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.com
workbook.save('Excel_Workbook.xls')
向单元格添加一个超链接

write_merge(x, x + m, y, w + n, string, sytle)
x表示行,y表示列,m表示跨行个数,n表示跨列个数,string表示要写入的单元格内容,style表示单元格样式。其中,x,y,w,h,都是以0开始计算的。

# 例子5 合并单元格
workbook = xlwt.Workbook()
worksheet = workbook.add_sheet('My Sheet',cell_overwrite_ok=True)
# # Merges row 0's columns 0 through 3.
worksheet.write_merge(0, 3, 0, 0, 'First Merge')
worksheet.write_merge(4, 7, 0, 0, 'Second Merge')  # 为什么是4,7 因为都是从0开始计算
workbook.save('Excel_Workbook.xls')
# 例子6 设置单元格对齐方式
workbook = xlwt.Workbook()
worksheet = workbook.add_sheet('My Sheet',cell_overwrite_ok=True)
alignment = xlwt.Alignment()
alignment.horz = xlwt.Alignment.HORZ_CENTER
alignment.vert = xlwt.Alignment.VERT_CENTER
style = xlwt.XFStyle()
style.alignment = alignment
# # Merges row 0's columns 0 through 3.
worksheet.write_merge(0, 3, 0, 0, 'First Merge',style)
worksheet.write_merge(4, 7, 0, 0, 'Second Merge')
workbook.save('Excel_Workbook.xls')
import xlwt
workbook = xlwt.Workbook()
worksheet = workbook.add_sheet('My Sheet')
alignment = xlwt.Alignment() # Create Alignment
alignment.horz = xlwt.Alignment.HORZ_CENTER # May be: HORZ_GENERAL, HORZ_LEFT, HORZ_CENTER, HORZ_RIGHT, HORZ_FILLED, HORZ_JUSTIFIED, HORZ_CENTER_ACROSS_SEL, HORZ_DISTRIBUTED
alignment.vert = xlwt.Alignment.VERT_CENTER # May be: VERT_TOP, VERT_CENTER, VERT_BOTTOM, VERT_JUSTIFIED, VERT_DISTRIBUTED
style = xlwt.XFStyle() # Create Style
style.alignment = alignment # Add Alignment to Style
worksheet.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 xlwt
workbook = xlwt.Workbook()
worksheet = workbook.add_sheet('My Sheet')
borders = xlwt.Borders() # Create Borders
borders.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.DASHED
borders.top = xlwt.Borders.DASHED
borders.bottom = xlwt.Borders.DASHED
borders.left_colour = 0x40
borders.right_colour = 0x40
borders.top_colour = 0x40
borders.bottom_colour = 0x40
style = xlwt.XFStyle() # Create Style
style.borders = borders # Add Borders to Style
worksheet.write(0, 0, 'Cell Contents', style)
workbook.save('Excel_Workbook.xls')
为单元格议添加边框
import xlwt
workbook = xlwt.Workbook()
worksheet = workbook.add_sheet('My Sheet')
pattern = xlwt.Pattern() # Create the Pattern
pattern.pattern = xlwt.Pattern.SOLID_PATTERN # May be: NO_PATTERN, SOLID_PATTERN, or 0x00 through 0x12
pattern.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 Pattern
style.pattern = pattern # Add Pattern to Style
worksheet.write(0, 0, 'Cell Contents', style)
workbook.save('Excel_Workbook.xls')
为单元格设置背景色

 

posted @ 2017-09-21 14:07  wanstack  阅读(618)  评论(0编辑  收藏  举报