#๐Ÿ”’ Python - Excel

10 messages ยท Page 1 of 1 (latest)

mellow dew
#

Hello, I'm trying to modify an Excel file using Python, but I'm losing the formatting. Is it possible to solve this problem?

cobalt ploverBOT
#

@mellow dew

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

mellow dew
#

Before

cobalt ploverBOT
#

Hey @mellow dew!

Please edit your message to use a code block

```py
print('Hello, world!')
```

This will result in the following:

print('Hello, world!')```
mellow dew
turbid spindle
#

As far as I know, pandas do not preserve the formatting

turbid spindle
#

But you can achieve using the openpyxl library

import openpyxl

# Load the existing workbook
wb = openpyxl.load_workbook("example.xlsx")

# Select the particular sheet
sheet = wb["Sheet1"]

# Modify the value in a specific cell
sheet["B2"] = 26

# Save the changes
wb.save("example.xlsx")
cobalt ploverBOT
#
Python help channel closed for inactivity

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.