找到一个识别表格颜色区的代码记录下来
代码引用来源:https://www.it1352.com/693919.html IT屋的交流区
感谢热心人分享的代码。
使用 xlrd 编写一个python脚本来从excel表中读取数据。工作表中的几个单元格以不同的颜色突出显示,想识别单元格的颜色代码。
方案一
import xlrd
book = xlrd.open_workbook("sample.xls", formatting_info=True)
sheets = book.sheet_names()
print "sheets are:", sheets
for index, sh in enumerate(sheets):
sheet = book.sheet_by_index(index)
print "Sheet:", sheet.name
rows, cols = sheet.nrows, sheet.ncols
print "Number of rows: %s Number of cols: %s" % (rows, cols)
for row in range(rows):
for col in range(cols):
print "row, col is:", row+1, col+1,
thecell = sheet.cell(row, col)
# could get 'dump', 'value', 'xf_index'
print thecell.value,
xfx = sheet.cell_xf_index(row, col)
xf = book.xf_list[xfx]
bgx = xf.background.pattern_colour_index
print bgx
方案二
import xlrd
book = xlrd.open_workbook(sample.xls,formatting_info = True)
sheets = book.sheet_names()
printsheet are:,sheet
for index,sh in enumerate(sheets):
sheet = book.sheet_by_index(index)
printSheet:,sheet.name
rows,cols = sheet.nrows,sheet.ncols
print行数:%s列数:%s%(rows,cols)
for range in rows(rows):
for col in range(cols):
printrow,col is:,row + 1,col + 1,
thecell = sheet.cell(row,col)
#可以得到'dump','value','xf_index'
print thecell.value,
xfx = sheet.cell_xf_index(row,col)
xf = book.xf_list [xfx]
bgx = xf.background.pattern_colour_index
print bgx
常用的格式操作代码:原文链接
代码引用来源 https://www.cnblogs.com/lisa2016/p/11158400.html一位牛人的博客
#!/usr/bin/env python
# -*- coding: utf-8 -*-” #只对当前文件的中文编码有效
# Filename : Write_excel_Format.py
import os
import time
import xlwt
#检测当前目录下是否有TestData2.xls文件,如果有则清除以前保存文件
filename = 'TestData2.xls'
if os.path.exists(filename):
os.remove(filename)
#打印读取到当前系统时间
print(time.strftime("%Y-%m-%d",time.localtime(time.time())))
wbk = xlwt.Workbook(encoding='utf-8')
sheet = wbk.add_sheet('new sheet 1', cell_overwrite_ok=True) #第二参数用于确认同一个cell单元是否可以重设值。
style = xlwt.XFStyle() #赋值style为XFStyle(),初始化样式
#设置居中
al = xlwt.Alignment()
al.horz = 0x02 # 设置水平居中
al.vert = 0x01 # 设置垂直居中
style.alignment = al
# 设置单元格背景颜色
for i in range(0x00,0xff): # 设置单元格背景颜色
pattern =xlwt.Pattern() # 创建一个模式
pattern.pattern = xlwt.Pattern.SOLID_PATTERN # 设置其模式为实型
pattern.pattern_fore_colour = i
# 设置单元格背景颜色 0 = Black, 1 = White, 2 = Red, 3 = Green, 4 = Blue, 5 = Yellow, 6 = Magenta, the list goes on...
style.pattern = pattern # 将赋值好的模式参数导入Style
Line_data = (u'测试表') #创建一个Line_data列表,并将其值赋为测试表,以utf-8编码时中文前加u
sheet.write_merge(i, i, 0, 2, Line_data, style) #以合并单元格形式写入数据,即将数据写入以第1/2/3列合并德单元格内
# 设置单元格内字体样式
for i in range(0x00,0xff):
fnt = xlwt.Font() # 创建一个文本格式,包括字体、字号和颜色样式特性
fnt.name = u'微软雅黑' # 设置其字体为微软雅黑
fnt.colour_index = i # 设置其字体颜色
fnt.bold = True
style.font = fnt #将赋值好的模式参数导入Style
#行合并
#以合并单元格形式写入数据,即将数据写入以第4/5/6列合并德单元格内
sheet.write_merge(i,i,3,5,Line_data,style)
# 设置单元格下框线样式
for i in range(0, 0x53):
borders = xlwt.Borders()
borders.left = i
borders.right = i
borders.top = i
borders.bottom = i
style.borders = borders #将赋值好的模式参数导入Style
sheet.write_merge(i,i,6,8,Line_data,style) #以合并单元格形式写入数据,即将数据写入以第4/5/6列合并德单元格内
# 设置单元格下列宽样式
for i in range(6, 15):
sheet.write(0,i,Line_data,style)
sheet.col(i).width = 0x0d00 + i*50
#设置居中
al = xlwt.Alignment()
al.horz = 0x02 # 设置水平居中
al.vert = 0x01 # 设置垂直居中
style.alignment = al
#行合并及列表合并
sheet.write_merge(2,2,11,13,"行合并3列",style)
sheet.write_merge(3,5,12,12,"列合并3列",style)
#插入图片
path_py = ".\images\python.bmp" #读取插入图片以.py运行时路径,images和.py在同一目录下
path_exe = ".\images\python.bmp" #读取插入图片以.exe运行时路径,.exe可以移到其他任意目录下运行但images和.exe在同一目录下
#path = cur_file_dir(path_py,path_exe) #获取文件的相对路径
path=os.path.abspath(path_py)
filename = path #检测当前目录下是否有python.bmp图片,
if os.path.exists(filename):
print(u'python.bmp图片存在')
else:
print(u'python.bmp图片不存在')
sheet.insert_bitmap(path, 2, 9) #插入一个图片
wbk.save('TestData2.xls') #保存TestData2.xls文件,保存到脚本或exe文件运行的目录下
input("Enter enter key to exit...") #插入一个输入命令,方便运行exe时一闪而过不到打印信息