生信技能20 - 生信分析结果文件Excel表的单元格自动化着色

通过Python实现excel表指定内容单元格着色,在输出文件中某列值超过标准范围或内容需要重点关注时,可以使用如下代码实现excel表内容的自动着色。

python代码

import openpyxl
import openpyxl.styles
from openpyxl.styles import PatternFill

# .xlsx文件列名称
list_columns = ['Chr',
                'Start',
                'End',
                'Ref',
                'Alt',
                'Func.refGene',
                'Gene.refGene',
                'GeneDetail.refGene',
                'ExonicFunc.refGene',
                'AAChange.refGene',
                'cytoBand',
                'CLNALLELEID',
                'CLNDN',
                'CLNDISDB',
                'CLNREVSTAT']
col_index = dict(zip(list_columns, list(range(1, len(list_columns) + 1))))

# 添加颜色函数
def add_color(file_path, output_path):
	  # 读取xlsx工作薄
    wb = openpyxl.load_workbook(file_path)
    # 读取sheet工作表
    sheet_name = wb.sheetnames
    sheet = wb[sheet_name[0]]
		
		# 定义着色颜色,可以自行修改颜色十六进制号
    fille_red = PatternFill("solid", fgColor="FF0000")
    fille_orange_low = PatternFill("solid", fgColor="FAD7A0")
    fille_orange_median = PatternFill("solid", fgColor="F39C12")
    fille_orange_high = PatternFill("solid", fgColor="D35400")
    fille_green_low = PatternFill("solid", fgColor="52BE80")
    for row in range(2, sheet.max_row + 1):
        # Alt
        col = col_index['Alt']
        alt_base = sheet.cell(row, col).value
        # 将Alt列全部着色为红色
        sheet.cell(row, col).fill = fille_red

        # Ref
        col = col_index['Ref']
        base = sheet.cell(row, col).value
        if base == 'A':
            sheet.cell(row, col).fill = fille_red
        elif base == 'T':
            sheet.cell(row, col).fill = fille_orange_high
        elif base == 'G':
            sheet.cell(row, col).fill = fille_orange_median
        elif base == 'C':
            sheet.cell(row, col).fill = fille_orange_low

        # ExonicFunc.refGene
        col = col_index['ExonicFunc.refGene']
        refGene = sheet.cell(row, col).value
        # 如果为exonic, 则将单元格着色为红色
        if refGene == 'exonic':
            sheet.cell(row, col).fill = fille_red

        # CLNDN
        col = col_index['CLNDN']
        result = sheet.cell(row, col).value
        # 如果not_provided在单元格中,则着色为绿色
        if 'not_provided' in result:
            sheet.cell(row, col).fill = fille_green_low
            
	  # 保存为新的xlxs表
    wb.save(output_path)


if __name__ == '__main__':
    input_file_path = "snpanno.hg19_multianno.xlsx"
    output_file_path = "snpanno.hg19_multianno_addcolor.xlsx"
    
		# 执行add_color函数
    add_color(input_file_path, output_file_path)

运行结果文件

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

生信与基因组学

每一份鼓励是我坚持下去动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值