返回总目录页

Python模块xlwt对excel进行写入操作

 

 python常用模块目录

 

1、安装

$ pip install xlwt

2、创建表格和工作表单写入内容

例子:

import xlwt
# 创建一个workbook 设置编码
workbook = xlwt.Workbook(encoding = 'utf-8') #print(workbook) %结果%<xlwt.Workbook.Workbook object at 0x005F4630>
# 创建一个worksheet
worksheet = workbook.add_sheet('小马过河')

# 写入excel
# 参数对应 行, 列, 值
worksheet.write(1,0, label = '第二行第一列')

# 保存
workbook.save('学习笔记.xls')
运行后 会在当前目录生成一个“学习笔记.xls”

3、表格带带样式单元格写入

import xlwt

workbook = xlwt.Workbook(encoding = 'ascii')
worksheet = workbook.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 # 设定样式
worksheet.write(0, 0, '不带样式的写入') # 不带样式的写入,第一行第一列
worksheet.write(1, 0, '带样式的写入', style) # 带样式的写入,第二行第一列
workbook.save('我的笔记.xls') # 保存文件

4、设置单元格宽度:

import xlwt

workbook = xlwt.Workbook()
worksheet = workbook.add_sheet('mcw')
worksheet.write(0, 0,'第一行第一列单元格内容')

# 设置单元格宽度
worksheet.col(0).width = 3333
workbook.save('cell_width.xls')

5、输入一个日期到单元格:

import xlwt
import datetime
workbook = xlwt.Workbook()
worksheet = workbook.add_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('输入日期.xlsx')

6、向单元格添加一个公式:

import xlwt
workbook = xlwt.Workbook()
worksheet = workbook.add_sheet('测试工作表')
worksheet.write(0, 0, 5) # Outputs 5  #第一行第一列5
worksheet.write(0, 1, 2) # Outputs 2  #第一行第二列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('添加公式并计算.xls')

 

7、向单元格添加一个超链接:

import xlwt
workbook = xlwt.Workbook()
worksheet = workbook.add_sheet('小马过河')
worksheet.write(0, 0, xlwt.Formula('HYPERLINK("https://www.baidu.com";"百度")')) # 输出文本百度,为超链接第一行第一列
workbook.save('添加超链接.xls')

8、合并列和行:

import xlwt
workbook = xlwt.Workbook()
worksheet = workbook.add_sheet('小马过河')
worksheet.write_merge(0, 1, 0, 3, '合并从第一行到第二行,第一列到第四列') # Merges row 0's columns 0 through 3.
font = xlwt.Font() # Create Font   #创建字体示例
font.bold = True # Set font to Bold  #bold设置为黑体字
style = xlwt.XFStyle() # Create Style
style.font = font # Add Bold Font to Style
worksheet.write_merge(2,3, 0, 3, '合并从第三行到第四行,第一列到第四列', style) # Merges row 1 through 2's columns 0 through 3.
workbook.save('合并单元格.xls')

9、设置单元格内容的对其方式:

#Alignment n. 结盟; 队列,排成直线; 校直,调整; [工] 准线;
#HORZ 水平的;地平式;
#GENERAL 普遍的; 大致的; 综合的; 总的,全体的;
#FILLED 满的; 填满的; 充气的; 加载的;
#JUSTIFIED adj. 有正当理由的,合理的; 事出有因的;
#DISTRIBUTED 分布式的;
#VERT n. 森林中的草木,绿色; 倾侧; 倾转;

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 # 将格式Alignment对象加入到样式对象Add Alignment to Style
worksheet.write(0, 0, '单元居中', style)  #写入的时候调用样式style
workbook.save('单元格对其方式.xls')
#创建alignment对象,设置单元格上下左右都是居中的。创建样式对象style,然后将alignment加入到style里面。在工作表worksheet指定单元格里写入数据,并使用style样式

10、为单元格议添加边框:

 # 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('小马过河')
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      #指定上下左右的边框颜色为0x40
borders.right_colour = 0x40
borders.top_colour = 0x40
borders.bottom_colour = 0x40
style = xlwt.XFStyle()  # Create Style   #创建样式对象
style.borders = borders  # 将设置好的边框对象borders 加到样式对象style中。Add Borders to Style
worksheet.write(0, 0, '单元格内容', style)   #向单元格第一行第一列写入“单元格内容”,并使用创建好的样式对象style
workbook.save('添加边框.xls')
#创建边框对象,对边框对象进行操作,指定边框上下左右的边框类型为虚线等等,指定上下左右的边框颜色为0x40。指定上下左右的边框颜色为0x40,将设置好的边框对象borders 加到样式对象style中。Add Borders to Style,#向单元格第一行第一列写入“单元格内容”,并使用创建好的样式对象style

11、为单元格设置背景色:

#SOLID 固体; 立体图形; 立方体;

import xlwt
workbook = xlwt.Workbook()
worksheet = workbook.add_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, '单元格内容', style)#向单元格写入内容时使用样式对象style
workbook.save('设置背景颜色.xls')

 

 

 

 

 

参考链接:
1)https://www.cnblogs.com/python-robot/p/9958352.html

 需要:

https://www.jb51.net/article/60510.htm

https://51changxue.com/python-excel%E6%93%8D%E4%BD%9C%E4%B9%8Bxlwt%E5%88%9B%E5%BB%BA%E8%A1%A8%E6%A0%BC/

https://www.cnblogs.com/Javauser/p/8950327.html

读取,写入,以及追加写入:https://www.jianshu.com/p/a8391a2b8c6c

posted @ 2019-04-19 19:11  马昌伟  阅读(30410)  评论(3编辑  收藏  举报
博主链接地址:https://www.cnblogs.com/machangwei-8/