Appending dataframe to the very same worksheet - python

I have been searching between different questions and answers regarding open openpyxl and xlsxwriter.
I have a simple question and I am sure there is a simple answer for it.
I have a dataframe which I am trying to append to an existing xlsx file. My issue is that it keeps creating a new worksheet everytime I run the code.
I have tried several solutions but none of them work. here is the code:
output_dict = {'Name': pd.Series(new_name),
'Link': pd.Series(new_url)
}
df = pd.DataFrame(output_dict)
with ExcelWriter('Links.xlsx', engine="openpyxl", mode='a') as writer:
df.to_excel(writer, sheet_name='in')
My question is how to append df to the very same worksheet called 'in'?

Try replacing your with ExcelWriter block with:
with pd.ExcelWriter('Links.xlsx',mode='a') as writer:
df.to_excel(writer, sheet_name='in')
Note the pd.ExcelWriter, not just ExcelWriter.
This is from the pandas documentation here. Second to last code block on the page.

Related

Trouble wrting to Excel

I' am new to Python and trying to write into a merged cell within Excel. I can see the data that is already stored within this cell/row, so I know its there. However when I try to overwrite it nothing happens.
I have tried messing with the index and header as well but nothing seems to work.
import pandas as pd
from openpyxl import load_workbook
Read the excel file into a pandas DataFrame
df = pd.read_excel(file here', sheet_name='Sheet1')
print(df.iloc[8, 2])
Make the changes to the DataFrame
df.iloc[8, 2] = "Bob Smith"
Load the workbook
book = load_workbook(file here)
writer = pd.ExcelWriter(file here, engine='openpyxl')
writer.book = book
Write the DataFrame to the first sheet
df.to_excel(writer, index=False)
Save the changes to the Excel file
writer.save()
import pandas as pd
from openpyxl import *
file="C:/Users/OneDrive/Bureau/draftExcel.xlsx"
df = pd.read_excel(file,sheet_name='sheet1')
df.iat[5,0]='cell is updated'
print(df) # to check first in the terminal if the content of the cell is updated
book=load_workbook(file)
writer=pd.ExcelWriter(file, engine='openpyxl')
df.to_excel(writer,sheet_name='sheet1',index=False)
writer.close()
I tried to make an example from what you explained because you didn't show your code, so I hope it was helpful.
Instead of using .iloc I used .iat so you can update the data in a specific cell in your DataFrame using column_index instead of column_label.
Remember that the Excel file you are working on must be closed while you are editing data with python, if it is open you will get an error.

Pandas ExcelWriter is overwriting my entire Excel workbook, instead of just appending to a worksheet

I'm trying to add my DataFrame data to an existing Excel worksheet and instead, it's completely overwriting it and deleting all of my existing worksheets. Here's the warning message I get in my console:
UserWarning: File contains an invalid specification for Closed POA&M Items. This will be removed
warn(msg)
UserWarning: File contains an invalid specification for Open POA&M Items. This will be removed
warn(msg)
I'm just trying to add to my "Open POA&M Items" sheet using the following code:
curr_poam = "Reports/September.xlsx"
curr_closed_sheet = "Open POA&M Items"
def main():
writeToExcel(closed_items, curr_poam, curr_closed_sheet)
def writeToExcel(dataframe, path_to_current_poam, sheet_name):
with pd.ExcelWriter(path_to_current_poam, engine="openpyxl", mode='a', if_sheet_exists='overlay') as writer:
dataframe.to_excel(writer, sheet_name=sheet_name, startrow=5, index=False, header=4)
Here's what my Open worksheet looks like, which has all the headers and columns formatted
Is it possible to start writing to this worksheet from row 6, adding my DataFrame to it? Based off the Pandas docs I thought I could just use mode='a' and if_sheet_exists='overlay' to append to an existing worksheet, but instead it's just deleting all worksheets and creating a new one with just my DataFrame in it.
Solutions attempted:
So far tried the two solutions here Pandas mode='a', if_sheet_exists='overlay' not working
Updating Pandas and Openpyxl didn't fix this problem for my use-case
Neither did adding a start row to this line of code dataframe.to_excel(writer, sheet_name=sheet_name, index=False, startrow=writer.sheets[sheet_name].max_row, header=None)
However, this did generate two new errors:
KeyError: 'Open POA&M Items'
IndexError: At least one sheet must be visible

Write dataframe to an existing excelfile without destrying it

ive got a problem.
I want to write a dataframe to an existing Excel-List which contains formulas.
When i Open a workbook and use a writer with pandas, it always says there is unreadable content in it and i need to repair it when i open the Excel-List.
Do you know how to resolve this?
Here is my code to write the list:
def Writer():
book = load_workbook(r'C:\Users\List.xlsx')
writer = pd.ExcelWriter(r'C:\Users\List.xlsx',header=None,
index=False, data_only=True)
writer.book = book
writer.sheets = dict((ws.title, ws) for ws in book.worksheets)
datafin=FindReqdata.datafin
datafin.to_excel(writer, sheet_name="SheetName", startrow=2,
startcol=5, index=None, header=None)
writer.save()
Writer()
have a look at this: https://stackoverflow.com/a/38075046/14367973
If i understood your question, you want to append more rows to a .xlsx file.
The new rows are from a dataFrame that have the same number of columns than the excel file.
If it is what you are trying to do the answer above should help you.
Keep the xlsx files closed while the script run sometimes it can break it.
Ok so apparently openpyxl has a problem with connection in the
excel spreadsheet. Because one Sheet has connections in it, the file is broken after editing it. I am still trying to fix this bug.

Writing text wrapped Excel Files using Python

I am new to Python and I was practicing by processing some CSV files and making an excel file from them. So far I can get the excel file however, I am unable to wrap the cells via python. I have tried multiple ways but none of it would work. Perhaps it is because of my poor understanding of Python. Can anyone suggest me how can I wrap text while writing the excel file? And please explain the code along the way? The error that i am getting for the following code is:
'str' object has no attribute 'alignment'
This is what I have done so far:
df=pd.DataFrame(list(zip(Dticketnumberlist,Dcategorylist)),
columns=['Ticket', 'Category'])
writer = pd.ExcelWriter('Trial Version.xlsx', engine='xlsxwriter')
df.to_excel(writer, sheet_name='Sheet1')
workbook=writer.book
worksheet = writer.sheets['Sheet1']
wrap_alignment = Alignment(wrap_text=True)
cell.alignment = wrap_alignment
You can use pandas with the xlsxwriter engine (the default).
You need to create a format object by calling the workbook.add_format() method as outlined in the xlsxwriter docs (link here).
Once you've used pandas.DataFrame.to_excel(), you can add the format using worksheet.set_column(). An example of this can be found in the xlsxwriter docs (link here).
I've provided a fully reproducible example below with the expected output.
import pandas as pd
df = pd.DataFrame({'Ticket': ['a','b','c','d'],
'Category': [2,1,4,3]})
writer = pd.ExcelWriter('Trial Version.xlsx', engine='xlsxwriter')
df.to_excel(writer, sheet_name='Sheet1', index=False)
workbook=writer.book
worksheet = writer.sheets['Sheet1']
format = workbook.add_format({'text_wrap': True})
# Setting the format but not setting the column width.
worksheet.set_column('A:B', None, format)
writer.save()
Expected Output:
Use python xlsxwriter
The question answered previously can help you better.

How to write to an Excel sheet without exporting a dataframe first?

I am trying to write some text to a specific sheet in an Excel file. I export a number of pandas dataframes to the other tabs, but in this one I need only some text - basically some comments explaining how the other tabs were calculated.
I have tried this but it doesn't work:
import pandas as pd
writer=pd.ExcelWriter('myfile.xlsx')
writer.sheets['mytab'].write(1,1,'This is a test')
writer.close()
I have tried adding writer.book.add_worksheet('mytab') and
ws=writer.sheets['mytab']
ws.write(1,1,'This is a test')
but in all cases I am getting: keyerror:'mytab'.
The only solution I have found is to write an empty dataframe to the tab before writing my text to the same tab:
emptydf=pd.DataFrame()
emptydf['x']=[None]
emptydf.to_excel(writer,'mytab',header=False, index=False)
I could of course create a workbook instance, as in the example on the documentation of xlsxwriter: http://xlsxwriter.readthedocs.io/worksheet.html
However, my problem is that I already have a pd.ExcelWriter instance, which is used in the rest of my code to create the other excel sheets.
I even tried passing a workbook instance to to_excel(), but it doesn't work:
workbook = xlsxwriter.Workbook('filename.xlsx')
emptydf.to_excel(workbook,'mytab',header=False, index=False)
Is there any alternative to my solution of exporting an empty dataframe - which seems as unpythonic as it can get?
You mentioned that you used add_worksheet() method from the writer.book object, but it seems to work and do what you wanted it to do. Below I've put in a reproducible example that worked successfully.
import pandas as pd
print(pd.__version__)
writer = pd.ExcelWriter('test.xlsx', engine='xlsxwriter')
workbook = writer.book
ws = workbook.add_worksheet('mytab')
ws.write(1,1,'This is a test')
writer.close()
Thought I'd also mention that I'm using pandas 0.18.1.

Categories