Sinestro 2022-06-14 11:33 采纳率: 87.5%
浏览 195
已结题

python excel 指定文字加粗

问题遇到的现象和发生背景

任务需要我将指定的文字加粗

img

问题相关代码,请勿粘贴截图
import re
import xlrd
import xlsxwriter



# 读取excel
data = xlrd.open_workbook('D:\huangzheliang\Python\农历-公历转化\待处理--清代灾赈时间预处理项填写说明.xlsx')
sheet = data.sheet_by_index(0)

# 写入excel

workbook = xlsxwriter.Workbook('D:\huangzheliang\Python\农历-公历转化\标注--清代灾赈时间预处理项填写说明.xlsx')
worksheet = workbook.add_worksheet('sheet1')


bold_red = workbook.add_format({'bold':True,'color':'red'})


for row in range(3,sheet.nrows):
    # 目标列
    target_Col = sheet.cell_value(row,4)
    # 处理列
    handle_Col = sheet.cell_value(row,3)
    # print(handle_Col)

    # 清除多余内容、拆分目标列
    target1 = target_Col.replace('【', '')
    target2 = target1.replace('】', '')
    target3 = target2.replace('(+)', '')
    target4 = target3.replace('(-)', '')
    target5 = re.sub('[A-Z]','',target4)
    target = target5.split(',')
    # print(target)
    listBold = []
    #遍历拆分后的列表,并分别拆出年号、年、月、日等信息
    for time in target:
        # print('完整日期:' + time)
        # 拆出年号
        year_number = time[0:2]
        
        # 排除空列表
        if time != '':
            # 定位年号
            # print('原:'+year_number)
            # print(handle_Col.find(year_number))
            if handle_Col.find(year_number) != -1:
                # print('新:'+year_number +'\n')
                final_year_number = year_number
                listBold.append(final_year_number)
                
            # 拆除年号后剩余内容
            year_and_rest = time[2:]
            # print('拆除年号后:' + year_and_rest)
            # 定位拆除年号后剩余内容
            # print(handle_Col.find(year_and_rest))
            if handle_Col.find(year_and_rest) != -1:
                # print(year_and_rest)
                # print(handle_Col.find(year_and_rest))
                final_year_and_rest = year_and_rest
                listBold.append(final_year_and_rest)
            
            # 若定位不到,则继续拆分
            if handle_Col.find(year_and_rest) == -1:
                # print(time)

                # 拆出年份
                # 如果time含有'年'
                if time.find('年') != -1:
                    # print(time)
                    yearList = year_and_rest.split('年')
                    year = yearList[0]
                    year2 = yearList[0] + '年'
                    # print(year)
                    # print(handle_Col.find(year))

                    if handle_Col.find(year2) != -1:
                        final_year = year2
                        listBold.append(final_year)
                    elif handle_Col.find(year) != -1:
                        final_year = year
                        listBold.append(final_year)

                    # 拆出年份后剩余内容
                    month_and_rest = yearList[1]
                    # print(month_and_rest)
                    # print(handle_Col.find(month_and_rest))
                    if handle_Col.find(month_and_rest) != -1:
                        final_month_and_rest = month_and_rest
                        listBold.append(final_month_and_rest)
                    # 若匹配不到则继续拆分
                    if handle_Col.find(month_and_rest) == -1:
                        # print(month_and_rest)

                        # 拆出月份
                        monthList = month_and_rest.split('月')
                        # print(monthList)
                        month = monthList[0]
                        # print(month)
                        # print(handle_Col.find(month))
                        if handle_Col.find(month) == -1:
                            final_month = month
                            listBold.append(final_month)
                        # 拆出月份后剩余内容
                        day_and_rest = monthList[1]
                        if monthList[1] != '':
                            # print(day_and_rest)
                            # print(handle_Col.find(day_and_rest))

                            # 拆出日期
                            dayList = day_and_rest.split('日')
                            # print(time)
                            # print(dayList)
                            for i in dayList:
                                if i != '':
                                    if handle_Col.find(i) != -1:
                                        final_day = i
                                        listBold.append(final_day)

                # 如果time不包含'年'
                if time.find('年') == -1:
                    # print(year_and_rest)
                    if len(year_and_rest) != 1:   
                        year = year_and_rest[0:2]
                        # print(year)
                        # print(handle_Col.find(year))
                        if handle_Col.find(year) != -1:
                            final_year = year
                            listBold.append(final_year)
                    # 拆出年份后剩余内容
                        month_and_rest = year_and_rest[2:]
                        # print(month_and_rest)
                        # print(handle_Col.find(month_and_rest))
                        if handle_Col.find(month_and_rest) != -1:
                            final_month_and_rest = month_and_rest
                            listBold.append(final_month_and_rest)
    # print(listBold)

    result_list = []
    for ele in listBold:
        if ele not in result_list:
            result_list.append(ele)
    # 需替换的文字列表
    print(result_list)
    result_str = ''.join(result_list)
    handle1 = handle_Col.replace('【', '')
    handle = handle1.replace('】', '')
    # 待替换的原文
    print('原文:'+handle + '\n')
    
    if len(result_list) != 0:
        for ele in result_list:
            if ele != '':
                replace_ele = 'bold_red,'+ ele
                # print(replace_ele)
                target_handle = re.sub(ele,replace_ele,handle)
                # print(target_handle +'\n')
    
    # strBold1 = ''.join(result_list)
    # strBold = re.sub('[{}]','',strBold1)
    # print(strBold1)
# savepath = 'D:\huangzheliang\Python\农历-公历转化\标注--清代灾赈时间预处理项填写说明.xls'
# book.save(savepath)
# print("==>批量保存成功....")
运行结果及报错内容
我的解答思路和尝试过的方法
我想要达到的结果

我希望能将拆分出的列表文字加粗后替换到原文,有没有什么好方法啊?

  • 写回答

2条回答 默认 最新

查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 6月22日
  • 已采纳回答 6月14日
  • 创建了问题 6月14日

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题