程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

python excel 指定文字加粗

編輯:Python
問題遇到的現象和發生背景

任務需要我將指定的文字加粗

問題相關代碼,請勿粘貼截圖
import reimport xlrdimport xlsxwriter# 讀取exceldata = xlrd.open_workbook('D:\huangzheliang\Python\農歷-公歷轉化\待處理--清代災赈時間預處理項填寫說明.xlsx')sheet = data.sheet_by_index(0)# 寫入excelworkbook = 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("==>批量保存成功....")
運行結果及報錯內容
我的解答思路和嘗試過的方法
我想要達到的結果

我希望能將拆分出的列表文字加粗後替換到原文,有沒有什麼好方法啊?


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved