I want to color a part of an Excel string in Python

Introduction

As the title says, it reads an Excel file using Python and outputs a part of the character string written in the cell with color. At first, I was planning to use openpyxl, but it seemed that I couldn't do what I wanted, so I decided to use ** write_rich_string ** of xlsxwriter to do it. However, xlsxwriter cannot read, so I will use openpyxl for the reading part and xlsxwriter for the writing part. I chose ~~ Python because I haven't touched it much. ~~

things to do

  1. Load Excel with Python

openpyxl


import openpyxl

#Open the file with openpyxl
iptbook = openpyxl.load_workbook(filename='test.xlsx')
#Sheet selection
iptsheet = iptbook.worksheets[0]
#Get cell string
cellvalue = iptsheet.cell(row=1, column=1).value
#End
iptbook.close()
  1. Color a part of the character string in the cell and output

xlsxwriter


import xlsxwriter

#Create a book with xlsxwriter
optbook = xlsxwriter.Workbook('opt.xlsx')
#Add sheet with xlsxwriter
optsheet = optbook.add_worksheet()
#Define format
red = optbook.add_format({'color': 'red'})
#Split characters
splitvalue = cellvalue.split()
#Write in rich text
optsheet.write_rich_string('A1', red, splitvalue[0], splitvalue[1])
#End
optbook.close()

result

The files are before and after execution.

bf.PNG

af.PNG

Articles that I used as a reference

Example: Writing “Rich” strings with multiple formats

I want to handle things in Excel where multiple formats (fonts) are mixed in one cell

in conclusion

With rich text, you can change not only the color but also the line weight and font. This time, it was realized by using two libraries in combination. If anyone knows a better way, I would be grateful if you could let me know.

Recommended Posts

I want to color a part of an Excel string in Python
I want to embed a variable in a Python string
I want to batch convert the result of "string" .split () in Python
I want to easily implement a timeout in python
I want to write in Python! (2) Let's write a test
I want to randomly sample a file in Python
I want to work with a robot in python.
I want to make input () a nice complement in python
I want to print in a comprehension
Create an instance of a predefined class from a string in Python
How to make a string into an array or an array into a string in Python
Get the formula in an excel file as a string in Python
I tried to create a Python script to get the value of a cell in Microsoft Excel
I want to do Dunnett's test in Python
How to embed a variable in a python string
I want to extract an arbitrary URL from the character string of the html source with python
I want to convert a table converted to PDF in Python back to CSV
I want to merge nested dicts in Python
I want to sort a list in the order of other lists
Open an Excel file in Python and color the map of Japan
I want to leave an arbitrary command in the command history of Shell
I made a program to check the size of a file in Python
I want to write to a file with Python
Insert an object inside a string in Python
Python: I want to measure the processing time of a function neatly
I tried to implement a card game of playing cards in Python
I want to display the progress in Python!
I want to do a monkey patch only partially safely in Python
I want to improve efficiency with Python even in an experimental system (3) I want to do something like Excel with Pandas
I want to create a priority queue that can be updated in Python (2.7)
I want to send Gmail with Python, but I can't because of an error
I made an appdo command to execute a command in the context of the app
I want to see a list of WebDAV files in the Requests module
I want to store the result of% time, %% time, etc. in an object (variable)
Python program is slow! I want to speed up! In such a case ...
I want to write in Python! (1) Code format check
How to convert / restore a string with [] in python
I want to iterate a Python generator many times
I want to generate a UUID quickly (memorandum) ~ Python ~
I want to transition with a button in flask
Cut a part of the string using a Python slice
[Python] I want to merge Excel files anyway (pandas.merge)
Even in JavaScript, I want to see Python `range ()`!
[Python] How to expand variables in a character string
I tried to implement a pseudo pachislot in Python
I want to split a character string with hiragana
I want to install a package of Php Redis
[Python] I want to make a nested list a tuple
I want to write in Python! (3) Utilize the mock
A collection of Excel operations often used in Python
I want to use the R dataset in python
I want to run a quantum computer with Python
I want to do something in Python when I finish
I want to manipulate strings in Kotlin like Python!
I tried to summarize the string operations of Python
Part 1 I wrote an example of the answer to the reference problem of how to write offline in real time in Python
I want to use a python data source in Re: Dash to get query results
I want to use a network defined by myself in PPO2 of Stable Baselines
I want to write a triple loop and conditional branch in one line in python
I want to specify another version of Python with pyvenv
Find out the apparent width of a string in python