Henry Damas
Henry Damas

Reputation: 1

How can I bold a list of strings an excel sheet using python

Hi I was wondering how I can bold a list of strings in a specific excel sheet column.

ex: I have the following dataset
dataset

and I want to bold the following words in the neighborhood column

words_to_bold: "Chinatown", "west village", "upper west side"

This is the code I have written so far, im just unsure as to how to on about doing this. Any suggestions are appreciated.

import pandas as pd

read_file = pd.read_csv (r'data.csv')
excel_file = read_file.to_excel (r'data.xlsx', index = None, header=True)

words_to_bold= ['Chinatown', 'west village', 'upper west side']

def list_bold(wordList, filename, sheet_name):
//unsure as to what to do now
//I also want to specify the sheet name as well. 

//calling function 
list_bold(words_to_bold, excel_file, sheet1)

Upvotes: 0

Views: 275

Answers (1)

Shivam Roy
Shivam Roy

Reputation: 2061

You can use openpyxl for this likewise:

from openpyxl import Workbook
from openpyxl.styles import Font
import openpyxl

words_to_bold= ['Chinatown', 'west village', 'upper west side']

wb = openpyxl.load_workbook('data.xlsx', read_only=True)
ws = wb.active

# Assuming the neighbourhood column is column F
for row in ws.iter_rows("F"):
    for cell in row:
        if cell.value.isin(words_to_bold):
            cell.font = Font(bold=True)
wb.save('data_bold.xlsx')

Upvotes: 1

Related Questions