千家信息网

python如何使用xlwt操作excel

发表于:2024-11-22 作者:千家信息网编辑
千家信息网最后更新 2024年11月22日,这篇文章主要讲解了"python如何使用xlwt操作excel",文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习"python如何使用xlwt操作excel
千家信息网最后更新 2024年11月22日python如何使用xlwt操作excel

这篇文章主要讲解了"python如何使用xlwt操作excel",文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习"python如何使用xlwt操作excel"吧!

一、安装

pip install xlwt

二、创建表格并写入

import xlwt# 创建一个workbook并设置编码workbook = xlwt.Workbook(encoding = 'utf-8')# 添加sheetworksheet = workbook.add_sheet('飞兔小哥')# 写入excel, 参数对应 行, 列, 值worksheet.write(1,0, label = '我爱autofelix')# 保存workbook.save('xlwt教学.xls')

三、设置单元格样式

import xlwtworkbook = 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, '带样式的单元格', style)# 保存文件workbook.save('xwlt单元格样式教学.xls')

四、设置单元格宽度

import xlwtworkbook = xlwt.Workbook()worksheet = workbook.add_sheet('飞兔小哥')worksheet.write(0, 0,'我爱autofelix')# 设置单元格宽度worksheet.col(0).width = 3000workbook.save('xwlt单元格宽度教学.xls')

五、设置单元格背景色

import xlwtworkbook = xlwt.Workbook()worksheet = workbook.add_sheet('飞兔小哥')# 创建模式对象Create the Patternpattern = xlwt.Pattern()# May be: NO_PATTERN, SOLID_PATTERN, or 0x00 through 0x12pattern.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 Patternstyle = xlwt.XFStyle()# 将模式加入到样式对象Add Pattern to Stylestyle.pattern = pattern#向单元格写入内容时使用样式对象styleworksheet.write(0, 0, '单元格内容', style)workbook.save('xwlt单元格背景色教学.xls')import xlwtworkbook = xlwt.Workbook()worksheet = workbook.add_sheet('飞兔小哥')# 创建模式对象Create the Patternpattern = xlwt.Pattern()# May be: NO_PATTERN, SOLID_PATTERN, or 0x00 through 0x12pattern.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 Patternstyle = xlwt.XFStyle()# 将模式加入到样式对象Add Pattern to Stylestyle.pattern = pattern#向单元格写入内容时使用样式对象styleworksheet.write(0, 0, '单元格内容', style)workbook.save('xwlt单元格背景色教学.xls')

六、设置单元格内容对齐方式

import xlwtworkbook = xlwt.Workbook()worksheet = workbook.add_sheet('飞兔小哥')# 创建对其格式的对象 Create Alignmentalignment = xlwt.Alignment()#水平居中 May be: HORZ_GENERAL, HORZ_LEFT, HORZ_CENTER, HORZ_RIGHT, HORZ_FILLED, HORZ_JUSTIFIED, HORZ_CENTER_ACROSS_SEL, HORZ_DISTRIBUTEDalignment.horz = xlwt.Alignment.HORZ_CENTER#我上下对齐 May be: VERT_TOP, VERT_CENTER, VERT_BOTTOM, VERT_JUSTIFIED, VERT_DISTRIBUTEDalignment.vert = xlwt.Alignment.VERT_CENTER #创建样式对象 Create Stylestyle = xlwt.XFStyle()# 将格式Alignment对象加入到样式对象Add Alignment to Stylestyle.alignment = alignment#写入的时候调用样式styleworksheet.write(0, 0, '单元居中', style)workbook.save('xlwt单元格对齐教学.xls')

七、单元格添加超链接

import xlwtworkbook = xlwt.Workbook()worksheet = workbook.add_sheet('飞兔小哥')worksheet.write(0, 0, xlwt.Formula('HYPERLINK("https://www.baidu.com";"百度")'))workbook.save('xwlt单元格超链接教学.xls')

八、单元格添加公式

import xlwtworkbook = xlwt.Workbook()worksheet = workbook.add_sheet('飞兔小哥')worksheet.write(0, 0, 5) # Outputs 5  #第一行第一列5worksheet.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('xwlt添加公式教学.xls')

九、单元格中输入日期

import xlwtimport datetimeworkbook = 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.0style.num_format_str = 'M/D/YY'worksheet.write(0, 0, datetime.datetime.now(), style)workbook.save('xwlt日期格式教学.xlsx')

十、合并行和列

import xlwtworkbook = xlwt.Workbook()worksheet = workbook.add_sheet('飞兔小哥')worksheet.write_merge(0, 1, 0, 3, '合并从第一行到第二行,第一列到第四列')#创建字体示例font = xlwt.Font()#bold设置为黑体字font.bold = True# Create Stylestyle = xlwt.XFStyle()# Add Bold Font to Stylestyle.font = fontworksheet.write_merge(2,3, 0, 3, '合并从第三行到第四行,第一列到第四列', style)workbook.save('xwlt合并单元格教学.xls')

十一、单元格添加边框

import xlwtworkbook = xlwt.Workbook()worksheet = workbook.add_sheet('飞兔小哥')# 创建边框对象Create Bordersborders = 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.DASHEDborders.right = xlwt.Borders.DASHEDborders.top = xlwt.Borders.DASHEDborders.bottom = xlwt.Borders.DASHED#指定上下左右的边框颜色为0x40borders.left_colour = 0x40borders.right_colour = 0x40borders.top_colour = 0x40borders.bottom_colour = 0x40# Create Style   #创建样式对象style = xlwt.XFStyle()# 将设置好的边框对象borders 加到样式对象style中。Add Borders to Stylestyle.borders = borders#向单元格第一行第一列写入"单元格内容",并使用创建好的样式对象styleworksheet.write(0, 0, '单元格内容', style)workbook.save('xwlt添加边框教学.xls')import xlwtworkbook = xlwt.Workbook()worksheet = workbook.add_sheet('飞兔小哥')# 创建边框对象Create Bordersborders = 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.DASHEDborders.right = xlwt.Borders.DASHEDborders.top = xlwt.Borders.DASHEDborders.bottom = xlwt.Borders.DASHED#指定上下左右的边框颜色为0x40borders.left_colour = 0x40borders.right_colour = 0x40borders.top_colour = 0x40borders.bottom_colour = 0x40# Create Style   #创建样式对象style = xlwt.XFStyle()# 将设置好的边框对象borders 加到样式对象style中。Add Borders to Stylestyle.borders = borders#向单元格第一行第一列写入"单元格内容",并使用创建好的样式对象styleworksheet.write(0, 0, '单元格内容', style)workbook.save('xwlt添加边框教学.xls')

感谢各位的阅读,以上就是"python如何使用xlwt操作excel"的内容了,经过本文的学习后,相信大家对python如何使用xlwt操作excel这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是,小编将为大家推送更多相关知识点的文章,欢迎关注!

0