Skip to content

Write pandas output to formatted excel file (don't lose it when writing it) #119

Closed

Description

@SSMK-wq

Hi, I am posting this here as I came to know about Styleframe from SO.

Would like to know whether it is possible to write a pandas output to formatted excel file?

I have a dataframe like as shown below

Date,cust,region,Abr,Number,         
12/01/2010,Company_Name,Somecity,Chi,36,
12/02/2010,Company_Name,Someothercity,Nyc,156,

df = pd.read_clipboard(sep=',')

I would like to write this dataframe to a specific sheet (called temp_data) in the file output.xlsx

Therfore I tried the below

import pandas
from openpyxl import load_workbook

book = load_workbook('output.xlsx')
writer = pandas.ExcelWriter('output.xlsx', engine='openpyxl') 
writer.book = book
writer.sheets = dict((ws.title, ws) for ws in book.worksheets)

I also tried the below

path = 'output.xlsx'

with pd.ExcelWriter(path) as writer:
    writer.book = openpyxl.load_workbook(path)
    final_df.to_excel(writer, sheet_name='temp_data',startrow=10)
writer.save()

But am not sure whether I am overcomplicating it. I get an error like as shown below. But I verifiedd in task manager, no excel file/task is running

BadZipFile: File is not a zip file

Moreover, I also lose my formatting of the output.xlsx file when I manage to write the file based on below suggestions. I already have a neatly formatted font,color file etc and just need to put the data inside.

Is there anyway to write the pandas dataframe to a specific sheet in an existing excel file? WITHOUT LOSING FORMATTING OF THE DESTIATION FILE

Activity

  1. DeepSpace2 commented on Mar 18, 2022

    @DeepSpace2
    Owner

    If I understand correctly, all you have to do is make sure you are using ExcelWriter with mode='a'.

    from styleframe import StyleFrame
    
    sf = StyleFrame({'b': [5, 6, 7, 8]})
    writer = StyleFrame.ExcelWriter('existing.xlsx', mode='a')
    sf.to_excel(writer, sheet_name='new sheet')
    writer.save()

    This will preserve all styling of existing sheets while adding a new sheet called 'new sheet'.

  2. SSMK-wq commented on Mar 18, 2022

    @SSMK-wq
    Author

    @DeepSpace2 - No, i wish to write to an already formatted sheet in an excel file. We manually did all the formatting like color, font, merged cells etc as a header. Now, i want my pandas output to be written to that specific formatted sheet ( where i start from row = 8). Hope this helps. I don't wish to add a new sheet

  3. SSMK-wq commented on Mar 18, 2022

    @SSMK-wq
    Author

    @DeepSpace2 - In addition to my comment above, when i specify the exact sheet that i want to write to with append mode and sheet exists = overlay, it strips all the formatting. Thats the problem. Or you mean to say that I should use StyleFrame dataframe only and not pandas dataframe?

  4. SSMK-wq commented on Mar 21, 2022

    @SSMK-wq
    Author

    @DeepSpace2 - Thanks, it works now. However, one question. Is it possible to write to the file keeping excel open? Currently, the permission is denied to write if I keep the file open. Is there anyway, that I can do this wriite operation by keeping the file open?

  5. DeepSpace2 commented on Mar 24, 2022

    @DeepSpace2
    Owner

    @SSMK-wq No, that's a limit imposed by your OS.

  6. Cata801 commented on Jan 14, 2024

    @Cata801

    @DeepSpace2 - No, i wish to write to an already formatted sheet in an excel file. We manually did all the formatting like color, font, merged cells etc as a header. Now, i want my pandas output to be written to that specific formatted sheet ( where i start from row = 8). Hope this helps. I don't wish to add a new sheet

    Hello @SSMK-wq
    I have exactly the same problem. Can you tell me how you solved it?

  7. Repository owner locked and limited conversation to collaborators on Jan 15, 2024
  8. converted this issue into a discussion #165 on Jan 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions