您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
这篇文章主要讲解了“python如何使用xlwt操作excel”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“python如何使用xlwt操作excel”吧!
pip install xlwt
import xlwt # 创建一个workbook并设置编码 workbook = xlwt.Workbook(encoding = 'utf-8') # 添加sheet worksheet = workbook.add_sheet('飞兔小哥') # 写入excel, 参数对应 行, 列, 值 worksheet.write(1,0, label = '我爱autofelix') # 保存 workbook.save('xlwt教学.xls')
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, '带样式的单元格', style) # 保存文件 workbook.save('xwlt单元格样式教学.xls')
import xlwt workbook = xlwt.Workbook() worksheet = workbook.add_sheet('飞兔小哥') worksheet.write(0, 0,'我爱autofelix') # 设置单元格宽度 worksheet.col(0).width = 3000 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 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('xwlt单元格背景色教学.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('xlwt单元格对齐教学.xls')
import xlwt workbook = xlwt.Workbook() worksheet = workbook.add_sheet('飞兔小哥') worksheet.write(0, 0, xlwt.Formula('HYPERLINK("https://www.baidu.com";"百度")')) workbook.save('xwlt单元格超链接教学.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('xwlt添加公式教学.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('xwlt日期格式教学.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('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 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('xwlt添加边框教学.xls')
感谢各位的阅读,以上就是“python如何使用xlwt操作excel”的内容了,经过本文的学习后,相信大家对python如何使用xlwt操作excel这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。