In Openpyxl, how to replace a cell with a Cell object in a worksheet?
up vote
-1
down vote
favorite
So I have a function which takes a Cell object and spit out a processed cell:
from openpyxl.cell.cell import Cell
cell = Cell(ws)
def process_cell(cell):
# Add style to cell
return cell
However I can't do this:
cell = process_cell(cell)
ws['A1'] = cell
The error is:
raise ValueError("Invalid column index {0}".format(idx))
ValueError: Invalid column index None
python openpyxl
|
show 1 more comment
up vote
-1
down vote
favorite
So I have a function which takes a Cell object and spit out a processed cell:
from openpyxl.cell.cell import Cell
cell = Cell(ws)
def process_cell(cell):
# Add style to cell
return cell
However I can't do this:
cell = process_cell(cell)
ws['A1'] = cell
The error is:
raise ValueError("Invalid column index {0}".format(idx))
ValueError: Invalid column index None
python openpyxl
1
Could you not justprocess_cell(ws['A1'])? It should change thestyleswithinws['A1']just fine as it's an object reference, not a value.
– Idlehands
Nov 19 at 17:24
It seems you're trying to replace the actualCellobjectws.cell(1,1)into your processedcellobject, which I would say is probably the wrong way to go about it. You'll be replacing all the valuablecellattributes with the defaultCellattributes as you only provided the minimalworksheetargument. Thecolumn,row,value... etc attributes will all be empty and therefore the cell is basically wiped with the exception of your explicitly added attributes inprocess_cell.
– Idlehands
Nov 19 at 17:49
You can forcibly replace thecellobject like thisws._cells[1, 1] = cellbut you will end up losing all the information from the existing cell. You should just be changing thews.cell(1, 1)object in place instead of assigning a new one.
– Idlehands
Nov 19 at 17:51
Read cell-styles-and-named-styles how to changecellstyles.
– stovfl
Nov 19 at 17:51
I'm following an example in the openpyxl tutorial: openpyxl.readthedocs.io/en/stable/pandas.html. In the writeOnlyCell example, it uses a generator to apply the cell style to every cell in the row. Itappendthe generator to the worksheet. The worksheet variablewsis only provided when the generator is created. And It has no problem appending the row to the worksheet.
– yughred
Nov 19 at 18:04
|
show 1 more comment
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
So I have a function which takes a Cell object and spit out a processed cell:
from openpyxl.cell.cell import Cell
cell = Cell(ws)
def process_cell(cell):
# Add style to cell
return cell
However I can't do this:
cell = process_cell(cell)
ws['A1'] = cell
The error is:
raise ValueError("Invalid column index {0}".format(idx))
ValueError: Invalid column index None
python openpyxl
So I have a function which takes a Cell object and spit out a processed cell:
from openpyxl.cell.cell import Cell
cell = Cell(ws)
def process_cell(cell):
# Add style to cell
return cell
However I can't do this:
cell = process_cell(cell)
ws['A1'] = cell
The error is:
raise ValueError("Invalid column index {0}".format(idx))
ValueError: Invalid column index None
python openpyxl
python openpyxl
asked Nov 19 at 16:57
yughred
13711
13711
1
Could you not justprocess_cell(ws['A1'])? It should change thestyleswithinws['A1']just fine as it's an object reference, not a value.
– Idlehands
Nov 19 at 17:24
It seems you're trying to replace the actualCellobjectws.cell(1,1)into your processedcellobject, which I would say is probably the wrong way to go about it. You'll be replacing all the valuablecellattributes with the defaultCellattributes as you only provided the minimalworksheetargument. Thecolumn,row,value... etc attributes will all be empty and therefore the cell is basically wiped with the exception of your explicitly added attributes inprocess_cell.
– Idlehands
Nov 19 at 17:49
You can forcibly replace thecellobject like thisws._cells[1, 1] = cellbut you will end up losing all the information from the existing cell. You should just be changing thews.cell(1, 1)object in place instead of assigning a new one.
– Idlehands
Nov 19 at 17:51
Read cell-styles-and-named-styles how to changecellstyles.
– stovfl
Nov 19 at 17:51
I'm following an example in the openpyxl tutorial: openpyxl.readthedocs.io/en/stable/pandas.html. In the writeOnlyCell example, it uses a generator to apply the cell style to every cell in the row. Itappendthe generator to the worksheet. The worksheet variablewsis only provided when the generator is created. And It has no problem appending the row to the worksheet.
– yughred
Nov 19 at 18:04
|
show 1 more comment
1
Could you not justprocess_cell(ws['A1'])? It should change thestyleswithinws['A1']just fine as it's an object reference, not a value.
– Idlehands
Nov 19 at 17:24
It seems you're trying to replace the actualCellobjectws.cell(1,1)into your processedcellobject, which I would say is probably the wrong way to go about it. You'll be replacing all the valuablecellattributes with the defaultCellattributes as you only provided the minimalworksheetargument. Thecolumn,row,value... etc attributes will all be empty and therefore the cell is basically wiped with the exception of your explicitly added attributes inprocess_cell.
– Idlehands
Nov 19 at 17:49
You can forcibly replace thecellobject like thisws._cells[1, 1] = cellbut you will end up losing all the information from the existing cell. You should just be changing thews.cell(1, 1)object in place instead of assigning a new one.
– Idlehands
Nov 19 at 17:51
Read cell-styles-and-named-styles how to changecellstyles.
– stovfl
Nov 19 at 17:51
I'm following an example in the openpyxl tutorial: openpyxl.readthedocs.io/en/stable/pandas.html. In the writeOnlyCell example, it uses a generator to apply the cell style to every cell in the row. Itappendthe generator to the worksheet. The worksheet variablewsis only provided when the generator is created. And It has no problem appending the row to the worksheet.
– yughred
Nov 19 at 18:04
1
1
Could you not just
process_cell(ws['A1'])? It should change the styles within ws['A1'] just fine as it's an object reference, not a value.– Idlehands
Nov 19 at 17:24
Could you not just
process_cell(ws['A1'])? It should change the styles within ws['A1'] just fine as it's an object reference, not a value.– Idlehands
Nov 19 at 17:24
It seems you're trying to replace the actual
Cell object ws.cell(1,1) into your processed cell object, which I would say is probably the wrong way to go about it. You'll be replacing all the valuable cell attributes with the default Cell attributes as you only provided the minimal worksheet argument. The column, row, value... etc attributes will all be empty and therefore the cell is basically wiped with the exception of your explicitly added attributes in process_cell.– Idlehands
Nov 19 at 17:49
It seems you're trying to replace the actual
Cell object ws.cell(1,1) into your processed cell object, which I would say is probably the wrong way to go about it. You'll be replacing all the valuable cell attributes with the default Cell attributes as you only provided the minimal worksheet argument. The column, row, value... etc attributes will all be empty and therefore the cell is basically wiped with the exception of your explicitly added attributes in process_cell.– Idlehands
Nov 19 at 17:49
You can forcibly replace the
cell object like this ws._cells[1, 1] = cell but you will end up losing all the information from the existing cell. You should just be changing the ws.cell(1, 1) object in place instead of assigning a new one.– Idlehands
Nov 19 at 17:51
You can forcibly replace the
cell object like this ws._cells[1, 1] = cell but you will end up losing all the information from the existing cell. You should just be changing the ws.cell(1, 1) object in place instead of assigning a new one.– Idlehands
Nov 19 at 17:51
Read cell-styles-and-named-styles how to change
cell styles.– stovfl
Nov 19 at 17:51
Read cell-styles-and-named-styles how to change
cell styles.– stovfl
Nov 19 at 17:51
I'm following an example in the openpyxl tutorial: openpyxl.readthedocs.io/en/stable/pandas.html. In the writeOnlyCell example, it uses a generator to apply the cell style to every cell in the row. It
append the generator to the worksheet. The worksheet variable ws is only provided when the generator is created. And It has no problem appending the row to the worksheet.– yughred
Nov 19 at 18:04
I'm following an example in the openpyxl tutorial: openpyxl.readthedocs.io/en/stable/pandas.html. In the writeOnlyCell example, it uses a generator to apply the cell style to every cell in the row. It
append the generator to the worksheet. The worksheet variable ws is only provided when the generator is created. And It has no problem appending the row to the worksheet.– yughred
Nov 19 at 18:04
|
show 1 more comment
1 Answer
1
active
oldest
votes
up vote
-1
down vote
The problem is this:
cell = Cell(ws)
This creates a cell bound to a particular worksheet but without any coordinates which you must supply. If you are in write-only mode then you must use a WriteOnlyCell and pass this in to the worksheet's append() method.
I tried a regularCelland it works well withappend. I just can't replace a single cell.
– yughred
Nov 20 at 16:25
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
-1
down vote
The problem is this:
cell = Cell(ws)
This creates a cell bound to a particular worksheet but without any coordinates which you must supply. If you are in write-only mode then you must use a WriteOnlyCell and pass this in to the worksheet's append() method.
I tried a regularCelland it works well withappend. I just can't replace a single cell.
– yughred
Nov 20 at 16:25
add a comment |
up vote
-1
down vote
The problem is this:
cell = Cell(ws)
This creates a cell bound to a particular worksheet but without any coordinates which you must supply. If you are in write-only mode then you must use a WriteOnlyCell and pass this in to the worksheet's append() method.
I tried a regularCelland it works well withappend. I just can't replace a single cell.
– yughred
Nov 20 at 16:25
add a comment |
up vote
-1
down vote
up vote
-1
down vote
The problem is this:
cell = Cell(ws)
This creates a cell bound to a particular worksheet but without any coordinates which you must supply. If you are in write-only mode then you must use a WriteOnlyCell and pass this in to the worksheet's append() method.
The problem is this:
cell = Cell(ws)
This creates a cell bound to a particular worksheet but without any coordinates which you must supply. If you are in write-only mode then you must use a WriteOnlyCell and pass this in to the worksheet's append() method.
answered Nov 20 at 9:01
Charlie Clark
9,55122233
9,55122233
I tried a regularCelland it works well withappend. I just can't replace a single cell.
– yughred
Nov 20 at 16:25
add a comment |
I tried a regularCelland it works well withappend. I just can't replace a single cell.
– yughred
Nov 20 at 16:25
I tried a regular
Cell and it works well with append. I just can't replace a single cell.– yughred
Nov 20 at 16:25
I tried a regular
Cell and it works well with append. I just can't replace a single cell.– yughred
Nov 20 at 16:25
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53379381%2fin-openpyxl-how-to-replace-a-cell-with-a-cell-object-in-a-worksheet%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Could you not just
process_cell(ws['A1'])? It should change thestyleswithinws['A1']just fine as it's an object reference, not a value.– Idlehands
Nov 19 at 17:24
It seems you're trying to replace the actual
Cellobjectws.cell(1,1)into your processedcellobject, which I would say is probably the wrong way to go about it. You'll be replacing all the valuablecellattributes with the defaultCellattributes as you only provided the minimalworksheetargument. Thecolumn,row,value... etc attributes will all be empty and therefore the cell is basically wiped with the exception of your explicitly added attributes inprocess_cell.– Idlehands
Nov 19 at 17:49
You can forcibly replace the
cellobject like thisws._cells[1, 1] = cellbut you will end up losing all the information from the existing cell. You should just be changing thews.cell(1, 1)object in place instead of assigning a new one.– Idlehands
Nov 19 at 17:51
Read cell-styles-and-named-styles how to change
cellstyles.– stovfl
Nov 19 at 17:51
I'm following an example in the openpyxl tutorial: openpyxl.readthedocs.io/en/stable/pandas.html. In the writeOnlyCell example, it uses a generator to apply the cell style to every cell in the row. It
appendthe generator to the worksheet. The worksheet variablewsis only provided when the generator is created. And It has no problem appending the row to the worksheet.– yughred
Nov 19 at 18:04