5

I have a excel file which has specific format, font and color etc. I would like to read it using Python libraries like pandas (pd.read_excel) and just modify a few cells without affecting the style. Is it possible? Currently, when I read and write using pandas, the style changes and it seems difficult to make the complex style in Python again.

Is there a way to load and store the format/style of Excel file when we are reading it, to be applied when it is being saved? I just want to modify the value of few cells.

1

2 Answers 2

6
+50

You can use the openpyxl library like this:

from openpyxl import Workbook, load_workbook

workbook = load_workbook("test.xlsx") # Your Excel file
worksheet = workbook.active # gets first sheet

for row in range(1, 10):
    # Writes a new value PRESERVING cell styles.
    worksheet.cell(row=row, column=1, value=f'NEW VALUE {row}')

workbook.save("test.xlsx")
Sign up to request clarification or add additional context in comments.

Comments

1

You can use the set_properties() function. Further use can be viewed at How to change the font-size of text in dataframe using pandas

1 Comment

Thanks but it is not just font, I would like to avoid headache of setting a complex style in Python. For example, read the format from excel and store with same format.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.