python xlwt写入excel操作

安装

$ pip install xlwt

例子:

import xlwt
# 创建一个workbook 设置编码
workbook = xlwt.Workbook(encoding = ‘utf-8‘)
# 创建一个worksheet
worksheet = workbook.add_sheet(‘My Worksheet‘)

# 写入excel
# 参数对应 行, 列, 值
worksheet.write(1,0, label = ‘this is test‘)

# 保存
workbook.save(‘Excel_test.xls‘)

运行后 会在当前目录生成一个Excel_test.xls

import xlwt

workbook = xlwt.Workbook(encoding = ‘ascii‘)
worksheet = workbook.add_sheet(‘My Worksheet‘)
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, ‘Unformatted value‘) # 不带样式的写入

worksheet.write(1, 0, ‘Formatted value‘, style) # 带样式的写入

workbook.save(‘formatting.xls‘) # 保存文件

效果:

设置单元格宽度:

import xlwt

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‘)

合并列和行:

import xlwt
workbook = xlwt.Workbook()
worksheet = workbook.add_sheet(‘My Sheet‘)
worksheet.write_merge(0, 0, 0, 3, ‘First Merge‘) # Merges row 0‘s columns 0 through 3.
font = xlwt.Font() # Create Font
font.bold = True # Set font to Bold
style = xlwt.XFStyle() # Create Style
style.font = font # Add Bold Font to Style
worksheet.write_merge(1, 2, 0, 3, ‘Second Merge‘, style) # Merges row 1 through 2‘s columns 0 through 3.
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‘)

原文地址:https://www.cnblogs.com/python-robot/p/9958352.html

时间: 2024-11-10 23:11:34

python xlwt写入excel操作的相关文章

通过python中xlrd读取excel表格(xlwt写入excel),xlsxwriter写入excel表格并绘制图形

1 import xlrd, xlwt 2 3 #读取excel文件 4 def read_excel(url):#传入源文件读取路径 5 # 获取数据 6 data = xlrd.open_workbook(url) 7 # 获取sheet 8 # table = data.sheet_by_name(sheet_name) #通过sheet名称获取sheet数据 9 table = data.sheet_by_index(0) #通过sheet索引获取sheet数据 10 # 获取总行数 1

使用Python xlwt写excel文件

如果需要使用Python写Excel文件,首先下载或者安装xlwt. pip install xlwt 下面的这些demo应该可以帮助开发者快速上手使用xlwt写Excel文件: 创建工作簿(workbook)和工作表(sheet): import xlwt workbook = xlwt.Workbook() sheet = workbook.add_sheet("Sheet Name") 写单元格(cell): sheet.write(0, 0, 'foobar') # row,

python xlwt 写excel文件

python 写excel文件,需要xlwt库.下载地址:https://pypi.python.org/pypi/xlwt/1.1.2 下载后修改扩展名为rar, 解压后安装: 安装成功后就可以引用了.如下代码: # -*- coding: utf-8 -*- import xlwt '''往EXCEl单元格写内容,每次写一行sheet:页签名称:row:行内容列表:rowIndex:行索引'''def WriteSheetRow(sheet,rowValueList,rowIndex): i

python常识系列07-->python利用xlwt写入excel文件

前言 读书之法,在循序而渐进,熟读而精思.--朱熹 抽空又来写一篇,毕竟知识在于分享! 一.xlwt模块是什么 python第三方工具包,用于往excel中写入数据:(ps:只能创建新表格,不能修改表格内容!!) 它支持 xlsx 和 xls 格式的excel表格: 与之对应的还有一个xlrd包,用于读取excel中的数据. 二.安装xlwt模块 命令行中输入:pip install xlwt 下载安装包进行离线安装 三.xlwt应用举例 3.1 xlwt基本应用实例 import xlwt #

python xlwt写excel格式控制 颜色、模式、编码、背景色

关于写excel的格式控制,比如颜色等等 1 import xlwt 2 from datetime import datetime 3 4 font0 = xlwt.Font() 5 font0.name = 'Times New Roman' 6 font0.colour_index = 2 7 font0.bold = True 8 9 style0 = xlwt.XFStyle() 10 style0.font = font0 11 12 style1 = xlwt.XFStyle()

python之数据驱动Excel操作(方法一)

一.Mail163.xlsx数据如下: 二.Mail163.py脚本如下 import xlrdimport unittestfrom selenium import webdriverfrom selenium.webdriver.common.by import Byimport time #安装:pip install xlrddef readExcel(nrow): '''读取Excel数据''' table = xlrd.open_workbook('Mail163.xlsx','r'

Python处理CSV,Excel,PDF和图片

使用Python处理CSV格式数据 CSV数据: 逗号分隔值(Comma-Separated Values,CSV,有时也称为字符分隔值,因为分隔字符也可以不是逗号),其文件以纯文本形式存储表格数据(数字和文本).纯文本意味着该文件是一个字符序列,不含必须像二进制数字那样被解读的数据.CSV文件由任意数目的记录组成,记录间以某种换行符分隔:每条记录由字段组成,字段间的分隔符是其它字符或字符串,最常见的是逗号或制表符.通常,所有记录都有完全相同的字段序列. 如一下格式:27,20,14,15,14

day7-Python学习笔记(十六)excel操作

import xlwt #写入excel# book = xlwt.Workbook() #新建一个excel# sheet = book.add_sheet('sheet1') #添加一个sheet页# sheet.write(0,0,'姓名')# sheet.write(0,1,'性别')# sheet.write(0,2,'年龄')# book.save('stu.xls') #微软的office不能用xlsx结尾的,wps随意 stus = [ ['姓名','年龄','性别','分数']

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('