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.
Related
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
I want to import an existing excel file and edit it. But when i copy the excel file and try to edit on it i get some errors. I did not get errors while trying to execute "write" command. But when i am trying to read some values in the cell, i am having problem.
import xlsxwriter
from xlrd import open_workbook
from xlwt import Workbook, easyxf
import xlwt
from xlutils.copy import copy
workbook=open_workbook("month.xlsx")
sheet=workbook.sheet_by_index(0)
print sheet.nrows
book = copy(workbook)
w_sheet=book.get_sheet(0)
print w_sheet.cell(0,0).value
Error: Traceback (most recent call last):
File "excel.py", line 18, in <module>
print w_sheet.cell(0,0).value
AttributeError: 'Worksheet' object has no attribute 'cell'
I haven't used this library, but looking at the documentation I think you are trying to do something it doesn't support. The worksheet documentation lists it's functionality and cell() is not there.
I think this library is for writing excel only, not reading.
Perhaps try pandas read_excel() to read the excel documents you create?
You can the use pandas iloc on the resulting dataframe to get the value you want:
value=pd.read_excel("file.xlsx", sheet_name="sheet").iloc[0,0]
I think that's correct, although I can't run the code to check just now...
I have 0.20.3 version of pandas install. I am trying to set header_style to false so that i can format the header row. xlsxwriter not applying format to header row of dataframe - Python Pandas
I keep getting error : AttributeError: 'module' object has no attribute 'formats'
I have tried
pd.formats.format.header_style = None
and
pd.core.format.header_style = None
Any idea what am I doing wrong ?
As you can see in the API, the module pandas.formats and pandas.core.format do not exist : https://pandas.pydata.org/pandas-docs/stable/api.html
It is normal that you have this error.
If you read new API changes with 0.20, pandas.formats has become pandas.io.formats. Try to check the API.
Another way to do this, suggested by #Martin Evans, is to write the headers directly, outside of Pandas. This avoids issues like above with different Pandas versions.
See also this example in the XlsxWriter docs.
Python 3.5 openpyxl 2.4
Hi everyone, I got a simple but confusing problem here.
FYI the API doc relating to worksheet is
http://openpyxl.readthedocs.io/en/default/api/openpyxl.worksheet.worksheet.html
Here is some simple code for testing.
# -*- coding: utf-8 -*-
from openpyxl import load_workbook
wb2 = load_workbook('example.xlsx')
print (wb2.get_sheet_names())
ws = wb2.get_sheet_by_name('Sheet1')
print (type(ws))
print (ws.calculate_dimension())
list = []
for i in ws.rows:
print ('\n')
for cell in i:
list.append(cell.value)
print(str(cell.value).encode('utf-8'))
print (type(ws))
ws.get_highest_row()
here's what turned out eventually
<class 'openpyxl.worksheet.worksheet.Worksheet'>
Traceback (most recent call last):
File "script.py", line 17, in <module>
ws.get_highest_row()
AttributeError: 'Worksheet' object has no attribute 'get_highest_row'
I run into the problem where it says that get_highest_row is not an attribute.
This seems correct since this function is under class worksheet.worksheet (from API doc), and ws is worksheet.worksheet.Worksheet (I've no idea what that is) may inherits some functions so it can still call dimension(), but can someone tell me how to fix this? I want to check through one specific row or column and do some sorting with varying length of cols and rows.
Any help is appreciated!
According to https://bitbucket.org/openpyxl/openpyxl/issues/278/get_highest_row-column-are-unreliable
In newest openpyxl, which has removed get_highest_row and get_highest_column method. They have been replaced by max_row and max_column property
I tried it with openpyxl 2.3.5 and got the following
/usr/local/lib/python3.5/site-packages/openpyxl/worksheet/worksheet.py:350:
UserWarning: Call to deprecated function or class get_highest_row (Use
the max_row property). def get_highest_row(self):
So as you are using 2.4 they probably removed it from there as it was deprecated already in 2.3.5.
EDIT: In the documentation for 2.4 this method is not mentioned any longer
I am already using an xlrd package. The code I am working on always returns an error message:
Traceback (most recent call last):
File "diffoct8.py", line 17, in <module>
row = rs.get(row_number)
AttributeError: 'Sheet' object has no attribute 'get'
What could be the problem?
Is there a newer version of XLRD?. If yes, how can I install it in Ubuntu?
Here you can get latest xlrd package. https://pypi.python.org/pypi/xlrd
From my understanding, you just want to get information from a row in a sheet. I assume there are 10 elements in a row.
Try this:
...
element_num = 10
row = []
for i in xrange(element_num):
row.append(rs.cell(row_number, i).value)
...
The method get() does not exist (It was purely used to show the approach you should take and where the problem was in your previous question). I've update my answer to that question to show you how you should use the row() method, as instructed in the documentation.