xlwt库是一个python用于操作excel的第三方库。它的主要功能是用来写入excel。通常会与xlrd 、 xlutils组合进行使用。
注意!这里的操作excel,实际上与excel无关,不需要下载excel,xlwt库直接操作的是excel打开的xls文件!注意!xlrd库只能创建和修改excel,不能打开excel!
可以使用pip进行安装,这是最简单也是最普遍的安装方式!
在cmd中输入pip install xlrd即可安装xlrd库。
安装成功后可以使用pip list来检查是否正确安装以及查看当前的xlrd版本。
注意:xlrd较高版本中不支持.xlsx文件,可以手动将版本降至更低的版本或者将.xlsx文件改为.xls文件先卸载掉现有的 xlrd —> pip uninstall xlrd重新安装指定版本的 xlrd —> pip install xlrd == 1.2.0接下来的文章由于此原因使用1.2.0版本的xlrd进行介绍
import xlwt
# 创建一个workbook并设置编码
workbook = xlwt.Workbook(encoding = 'utf-8')
# 添加sheet
worksheet = workbook.add_sheet('微课列表')
# 写入excel, 参数对应 行, 列, 值
worksheet.write(1,0, label = 'MySQL零基础入门课程')# 保存
workbook.save('W3Cschool课程内容.xls')
import xlwt
workbook = xlwt.Workbook(encoding = 'utf-8')
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, '带样式的单元格', style)
# 保存文件
workbook.save('W3Cschool课程内容.xls')
import xlwt
workbook = xlwt.Workbook()
worksheet = workbook.add_sheet('微课列表')
worksheet.write(1, 1,'MySQL')
# 设置单元格宽度
worksheet.col(0).width = 3000
workbook.save('W3Cschool课程内容.xls')
import xlwt
workbook = xlwt.Workbook()
worksheet = workbook.add_sheet('微课列表')
# 创建模式对象Create the Pattern
pattern = xlwt.Pattern()
# May be: NO_PATTERN, SOLID_PATTERN, or 0x00 through 0x12
pattern.pattern = xlwt.Pattern.SOLID_PATTERN
#设置模式颜色 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...
pattern.pattern_fore_colour = 5
# 创建样式对象Create the Pattern
style = xlwt.XFStyle()
# 将模式加入到样式对象Add Pattern to Style
style.pattern = pattern
#向单元格写入内容时使用样式对象style
worksheet.write(0, 0, '单元格内容', style)
workbook.save('W3Cschool课程内容.xls')
import xlwt
workbook = xlwt.Workbook()
worksheet = workbook.add_sheet('微课列表')
# 创建对其格式的对象 Create Alignment
alignment = xlwt.Alignment()
#水平居中 May be: HORZ_GENERAL, HORZ_LEFT, HORZ_CENTER, HORZ_RIGHT, HORZ_FILLED, HORZ_JUSTIFIED, HORZ_CENTER_ACROSS_SEL, HORZ_DISTRIBUTED
alignment.horz = xlwt.Alignment.HORZ_CENTER
#我上下对齐 May be: VERT_TOP, VERT_CENTER, VERT_BOTTOM, VERT_JUSTIFIED, VERT_DISTRIBUTED
alignment.vert = xlwt.Alignment.VERT_CENTER
#创建样式对象 Create Style
style = xlwt.XFStyle()
# 将格式Alignment对象加入到样式对象Add Alignment to Style
style.alignment = alignment
#写入的时候调用样式style
worksheet.write(0, 0, '单元居中', style)
workbook.save('W3Cschool课程内容.xls')
import xlwt
workbook = xlwt.Workbook()
worksheet = workbook.add_sheet('')
worksheet.write(2, 3, xlwt.Formula('HYPERLINK("https://www.w3cschool.cn/minicourse/play/txymysql";"MySQL零基础入门课程")'))
workbook.save('W3Cschool课程内容.xls')
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
#(A1[5] * A2[2]) 第二行第一列,输出第一行数字乘积
worksheet.write(1, 0, xlwt.Formula('A1*B1'))
#(A1[5] + A2[2]) 第二行第二列,输出第一行数字之和
worksheet.write(1, 1, xlwt.Formula('SUM(A1,B1)'))
workbook.save('W3Cschool课程内容.xls')
import xlwt
import datetime
workbook = xlwt.Workbook()
worksheet = workbook.add_sheet('微课列表')
style = xlwt.XFStyle()
#其他格式包括: 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
style.num_format_str = 'M/D/YY'
worksheet.write(0, 0, datetime.datetime.now(), style)
workbook.save('W3Cschool课程内容.xlsx')
import xlwt
workbook = xlwt.Workbook()
worksheet = workbook.add_sheet('微课列表')
worksheet.write_merge(0, 1, 0, 3, '合并从第一行到第二行,第一列到第四列')
#创建字体示例
font = xlwt.Font()
#bold设置为黑体字
font.bold = True
# Create Style
style = xlwt.XFStyle()
# Add Bold Font to Style
style.font = font
worksheet.write_merge(2,3, 0, 3, '合并从第三行到第四行,第一列到第四列', style)
workbook.save('W3Cschool课程内容.xls')
import xlwt
workbook = xlwt.Workbook()
worksheet = workbook.add_sheet('微课列表')
# 创建边框对象Create Borders
borders = xlwt.Borders()
#对边框对象进行操作,指定边框上下左右的边框类型为虚线
# 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.left = xlwt.Borders.DASHED
borders.right = xlwt.Borders.DASHED
borders.top = xlwt.Borders.DASHED
borders.bottom = xlwt.Borders.DASHED
#指定上下左右的边框颜色为0x40
borders.left_colour = 0x40
borders.right_colour = 0x40
borders.top_colour = 0x40
borders.bottom_colour = 0x40
# Create Style #创建样式对象
style = xlwt.XFStyle()
# 将设置好的边框对象borders 加到样式对象style中。Add Borders to Style
style.borders = borders
#向单元格第一行第一列写入“单元格内容”,并使用创建好的样式对象style
worksheet.write(0, 0, '单元格内容', style)
workbook.save('W3Cschool课程内容.xls')