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









share|improve this question


















  • 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












  • 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












  • 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

















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









share|improve this question


















  • 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












  • 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












  • 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















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









share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 19 at 16:57









yughred

13711




13711








  • 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












  • 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












  • 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
















  • 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












  • 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












  • 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










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














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.






share|improve this answer





















  • 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











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















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

























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.






share|improve this answer





















  • 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















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.






share|improve this answer





















  • 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













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.






share|improve this answer












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.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 20 at 9:01









Charlie Clark

9,55122233




9,55122233












  • 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
















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


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














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





















































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







Popular posts from this blog

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

TypeError: fit_transform() missing 1 required positional argument: 'X'