Write in existing Excel via Python


Thu Jan 19 2023 20:39:42 GMT+0000 (Coordinated Universal Time)

Saved by @tofufu #python

import openpyxl
import string

ses["writeable_excel"] = openpyxl.load_workbook("/path/to/Excel.xlsx")
ses["writeable_excel_active"] = ses["writeable_excel"].active

col1 = "Column 1"
col3 = "Column 3"
col6 = "Column 6"

excel_cols = list(string.ascii_uppercase)
database_cols = [col1, col3, col6]

# If copying col names fr a different Excel, make sure they are up-to-date
for col in range(0, len(database_cols)):
   ses["writeable_excel_active"][excel_cols[col] + str(1)].value = database_cols[col]	# .strip(), if some cells have whitespace, take it out so that it'll show as NaN

ses["writeable_excel_read"] = pd.read_excel("/path/to/Excel.xlsx", engine="openpyxl")   # read written Excel