I am trying to write a code that will be able to replace cells in an existing excel sheet with a dataframe values. The lines work, but the problem is: that the codes require pythoncom library, a windows base library. So when I tried to upload it to streamlit cloud, which is based on Linux, an error arose. the code is like this:
pythoncom.CoInitialize()
with xw.App() as app:
wb=xw.Book(path)
wb.sheets(sheet_name).range(kolom_cluster+str(header+1)).options( index=False,chunksize=30_000).value=df["cluster"]
wb.save(path)
wb.close()
Therefore, I am wondering if there is an alternative to doing the same (writing df values to an existing excel file) without the need for pythoncom?
Thank you very much for your kind attention and solution.
This is my first question in Stackoverflow. Have been using it for quite a while. I have tried to search for the same solution over StackOverflow quite some time. Thus, I am hoping, if you guys could help me solve the problem.
Thank you very much.
PS. I have tried script like this, using openpyxl
writer = pd.ExcelWriter(path, engine='openpyxl',
mode='a', # append data instead of overwriting all the book by default
if_sheet_exists='overlay' # write the data over the old one instead of raising a ValueError
)
df["cluster"].to_excel(
writer,
sheet_name=sheet_name,
startrow=header+1, # upper left corner where to start writing data
startcol=ord(kolom_cluster), # note that it starts from 0, not 1 as in Excel
index=False, # don't write index
header=False # don't write headers
)
writer.save()
writer.close()
But it returns some error: