Writer.book = ExcelWorkbook cant set attribute 'book' - python

ExcelWorkbook = py.load_workbook(FilePath)
writer = pd.ExcelWriter(FilePath, engine = 'openpyxl')
writer.book = ExcelWorkbook
on one pc this runs perfectly fine on the other one
I get an error
AttributeError :can't set attribute 'book' on line 3
I'm lost on how to fix this as it works fine on one but not the other I also made sure to check that openpyxl was installed on the pc that it is not working on
is this the only way to combine 2 dataframes into a workbook that has separate sheets for each dataframe?

Check your Pandas version on both computers.
I had the same issue and it seems like it is related to Pandas 1.5.0.
I removed it and used
pip install pandas==1.4.4
that fixed it for me

Related

AttributeError: 'Worksheet' object has no attribute 'set_default_row'

I am getting an error that seems weird. Worksheet object does have set_default_row() function, in the docs. Not sure what I am missing here.
I got this code project from someone who made this and has been running for a long time. We are using different python versions. He's on 3.10 and I am on 3.9 and I don't see that to be any reason.
Error:
Traceback (most recent call last):
File "C:\Users\ajoshi\my_folder\misc\quick tools\CI-TestRun-Report-Generator\FileProvider.py", line 31, in create
worksheet.set_default_row(20)
AttributeError: 'Worksheet' object has no attribute 'set_default_row'
Code:
s = data.style.applymap(FileProvider.color_negative_red)
s.to_excel(writer, sheet_name=plan["name"], header=True, index=False)
workbook = writer.book
worksheet = writer.sheets[plan["name"]]
worksheet.set_default_row(20)
worksheet.set_row(0, 40)
The issue is that you are calling a xlsxwriter method but that, most probably, the module isn't installed so Pandas is defaulting to creating a openpyxl worksheet object which has different APIs and doesn't have that method. Try set up your Pandas xlsx writer like this:
writer = pd.ExcelWriter('filename.xlsx', engine='xlsxwriter')
If that fails then you need to install xlsxwriter.
If you are already using engine='xlsxwriter' then the issue could be that you have a very old version installed that doesn't support the set_default_row() method. In which case you should upgrade xlsxwriter.

<ValueError: Sheet already exists and if_sheet_exists is set to 'error'> on one machine but not on another

I'm running some basic python code which generates a pandas DataFrame called df and then writes it to a pre-formatted Excel file using pandas ExcelWriter and openpyxl as its engine.
workbook = load_workbook('example.xlsx')
sheet = workbook['example_sheet']
writer = pd.ExcelWriter('example.xlsx', engine='openpyxl', mode='a')
writer.book = workbook
writer.sheets = {ws.title: ws for ws in writer.book.worksheets}
sheet.cell(row=8, column=5).value = some.value
df.to_excel(writer, sheet_name='example_sheet', index=False, header=False, startrow=10, startcol=5)
The strange thing is that when running on my machine it works perfectly, however on my colleague's machine it throws the mentioned error:
File "C:\Users\user\AppData\Local\Programs\Python\Python37\lib\site-packages\pandas\io\excel\_openpyxl.py", line 437, in write_cells f"Sheet '{sheet_name}' already exists and " ValueError: Sheet 'example_sheet' already exists and if_sheet_exists is set to 'error'.
I've tried working around it by explicitly setting
if_sheet_exists = 'replace'
however now (as expected) it replaces the entire sheet and destroys the formatting that was applied before. When running on my machine however it would not replace the sheet, even though the writer was set to do so.
I'm not entirely sure where to look for differences in the machines so if anyone could throw me an idea I would be very thankful.
The last Pandas version to behave as you expect was version 1.2.5. pip uninstall pandas and then pip install pandas==1.2.5, then it will simply append the data to your previously formatted Excel template.
YMMV.
Edit:
I believe the issue was with openpyxl, which you may be able to revert separately if the above solution is not feasible. You might also try if_sheet_exists = 'overlay' if it suits your needs.

Python appending dataframe to exsiting excel file and sheet

I have a question about appending a dataframe to existing sheet on existing file.
I tried to write the code by myself
writer = pd.ExcelWriter('existingFile.xlsx', engine='openpyxl', mode='a')
df.to_excel(writer, sheet_name="existingSheet", startrow=writer.sheets["existingSheet"].max_row, index=False, header=False)
and this would cause an error
ValueError: Sheet 'existingSheet' already exists and if_sheet_exists is set to 'error'.
and I googled and found this function in here;
Append existing excel sheet with new dataframe using python pandas
and even with this function, it still causes the same error, even though i think this function prevents this exact error from what i think.
Could you please help?
Thank you very much!
I guess I am late to the party but
Add keyword if_sheet_exists='replace' to ExcelWriter like this
pd.ExcelWriter('existingFile.xlsx', engine='openpyxl', mode='a', if_sheet_exists='replace' )
Then you can use the latest version of pandas
Since Pandas 1.4, there is also an 'overlay' option to if_sheet_exists.
It seems this function is broken in pandas 1.3.0 Just look at this document , when trying to append to existing sheet it will either raise an error, create a new one or replace it
if_sheet_exists{‘error’, ‘new’, ‘replace’}, default ‘error’ How to
behave when trying to write to a sheet that already exists (append
mode only).
error: raise a ValueError.
new: Create a new sheet, with a name determined by the engine.
replace: Delete the contents of the sheet before writing to it.
New in version 1.3.0
The only solution is to downgrade pandas (1.2.3 is working for me)
pip install pandas==1.2.3

Using add_table() method in openpyxl corrupts an existing excel file

I have an existing excel file called "File.xlsx" with a sheet called "MySheet"
The data in MySheet is currently a range, I would like to open the excel file and convert the data in MySheet to an excel table. I am succesfully able to do this part, but when I open the excel file I get an error message that reads: Repaired Records: Table from /xl/tables/table1.xml part (Table)
The script works in creating the table but I would like to avoid the excel file being unreadable error.
The script I am using is here:
from openpyxl.worksheet.table import Table
from openpyxl.utils import get_column_letter
file_name = "File.xlsx"
wb = load_workbook(file_name)
ws = wb['MySheet']
max_row = ws.max_row
max_column = ws.max_column
table = Table(displayName="FailureData", ref="A1:" + get_column_letter(max_column) + str(max_row))
ws.add_table(table)
wb.save(file_name)
wb.close()
In my case, the error occurs when 2 header field has the same name. The solution is to make sure to have unique field names in the header by incrementing 1 on each duplication:
f = ["Ford", "Volvo", "BMW", "Ford", "Ford"]
print(f)
>>['Ford', 'Volvo', 'BMW', 'Ford', 'Ford']
fields_out = [(x if i == f.index(x) else x + str(f.count(x) - f[i + 1:].count(x))) for i, x in enumerate(f)]
print(fields_out)
>>['Ford', 'Volvo', 'BMW', 'Ford2', 'Ford3']
For the same operation via the interface, Excel modifies the name of the duplicate fields.
openpyxl version 3.0.5
I figured it out. It seems to be an issue with the older vesion of openpyxl, I was using version 2.6.1. I updated to the latest version 3.0.4 and the error no longer shows up.
I stumbled across this having the same error. My issue was that my table names started with numbers. Excel doesn't like that apparently.
I know this has been a while, but for anyone else coming across this, I had a similar problem. The table had two columns with the same name. I dropped the duplicate column from the df and that resolved the issue.

"Worksheet range names does not exist" KeyError in openpyxl

Let me preface this by saying I have tried looking for, and cannot seem to find a similar situation so please don't be too upset if this seems familiar to you. I am using Python 2.7 and openpyxl version 2.2.5 (I need to use 2.7, and used an older module for other reasons.)
I am new to Python and read/write code in general, so I'm testing this on the command line before I implement it:
I created a file, foo.xlsx in the Python27 file directory with some values that I manually entered via Excel.
I then used this simple code on the Python command line to test my code
from openpyxl import load_workbook
wb = load_workbook('foo.xlsx')
sheet_ranges = wb['range names']
It then resulted in the following error:
File "C:\Python27\lib\openpyxl\workbook.workbook.py", line 233 in getitem
raise KeyError("Worksheet {0} does not exist.".format(key))
KeyError: 'Worksheet sheet range names does not exist'
So I thought it had something to do with not importing the entire openpyxl module. I proceeded to do that and run the whole process but it resulted in the same error.
Can someone please let me know what I am doing wrong/how to solve this?
Additional information:
I had successfully written to an empty file before, and then read the values. This gave me the right values for everything EXCEPT what I had written in manually via Excel- the cells that had manual input returned None or Nonetype. The issue seems to be with cells with manual input.
I did hit save on the file before accessing it don't worry
This was in the same directory so I know that it wasn't a matter of location.
The following command does not make sense:
sheet_ranges = wb['range names']
Normally you open a workbook and then access one of the worksheets, the following gives you some examples on how this can be done:
import openpyxl
wb = openpyxl.Workbook()
wb = openpyxl.load_workbook(filename = 'input.xlsx')
# To display all of the available worksheet names
sheets = wb.sheetnames
print sheets
# To work with the first sheet (by name)
ws = wb[sheets[0]]
print ws['A1'].value
# To work with the active sheet
ws = wb.active
print ws['A1'].value
# To work with the active sheet (alternative method)
ws = wb.get_active_sheet()
print ws['A1'].value
If you want to display any named range in the workbook, you can do the following:
print wb.get_named_ranges()
I'm not exactly sure what it is you need to do, but to read Excel spreadsheets into python, I usually use xlrd (which to me was easier to get use to). See example:
import xlrd
workbook = xlrd.open_workbook(in_fname)
worksheet = workbook.sheet_by_index(0)
To write to Excel spreadsheets, I use xlsxwriter:
import xlsxwriter
workbook = xlsxwriter.Workbook(out_fname)
worksheet = workbook.add_worksheet('spreadsheet_name')
Hope this helps.

Categories