OpenPyXl: Open an existing spreadsheet in write_only mode
I am suffering from memory consumption issues when streaming data into an existing Excel template using OpenPyXl.
My process is as follows:
1. Open existing template
2. Stream data into 3 different sheets
3. Save the workbook and download to the user
Code below:
path = r'template.xlsx'
output = BytesIO()
# Saving to Excel
book = load_workbook(path)
days_ws = book['Days']
for r in dataframe_to_rows(days_df, index=True, header=True):
days_ws.append(r)
rates_ws = book['Daily Rates']
for r in dataframe_to_rows(rates_df, index=True, header=True):
rates_ws.append(r)
settings_ws = book['Settings']
for r in dataframe_to_rows(settings_df, index=True, header=True):
settings_ws.append(r)
with NamedTemporaryFile() as tmp:
book.save(tmp.name)
tmp.seek(0)
stream = tmp.read()
What I would like to achieve is the ability to open an existing spreadsheet in write_only mode, to allow for data streaming into the workbooks without huge memory overhead. I know this is possible when creating worksheets, however I have not been able to find an example using an existing one.
I would be happy to work with any libraries or modules outside of OpenPyXl if anyone has other suggestions. Cheers!
Thanks for your help in advance!
python excel openpyxl xlsxwriter
|
show 6 more comments
I am suffering from memory consumption issues when streaming data into an existing Excel template using OpenPyXl.
My process is as follows:
1. Open existing template
2. Stream data into 3 different sheets
3. Save the workbook and download to the user
Code below:
path = r'template.xlsx'
output = BytesIO()
# Saving to Excel
book = load_workbook(path)
days_ws = book['Days']
for r in dataframe_to_rows(days_df, index=True, header=True):
days_ws.append(r)
rates_ws = book['Daily Rates']
for r in dataframe_to_rows(rates_df, index=True, header=True):
rates_ws.append(r)
settings_ws = book['Settings']
for r in dataframe_to_rows(settings_df, index=True, header=True):
settings_ws.append(r)
with NamedTemporaryFile() as tmp:
book.save(tmp.name)
tmp.seek(0)
stream = tmp.read()
What I would like to achieve is the ability to open an existing spreadsheet in write_only mode, to allow for data streaming into the workbooks without huge memory overhead. I know this is possible when creating worksheets, however I have not been able to find an example using an existing one.
I would be happy to work with any libraries or modules outside of OpenPyXl if anyone has other suggestions. Cheers!
Thanks for your help in advance!
python excel openpyxl xlsxwriter
You can combineread-only
andwrite-only
modes.
– Charlie Clark
Nov 26 '18 at 10:46
@CharlieClark thanks for your reply. I believe that is what is currently happening, and is the reason why my memory usage is so high. I am looking to reduce the memory consumption of the process. Currently it's ~600mb of memory to save a 2.3mb excel sheet.
– Tom Farrand
Nov 26 '18 at 11:48
No, it's not what you're currently doing.wb1 = load_workbook(path, read_only=True)
would be needed for that.wb2 = Workbook(write_only=True)
– Charlie Clark
Nov 26 '18 at 12:06
@CharlieClark if I understand correctly, that would open up my existing workbook in read only mode- preventing me from streaming data to it. Whilewb2 = Workbook(write_only=True)
would create a completely separate new workbook which I could then write data into.
– Tom Farrand
Nov 26 '18 at 12:36
That's the way to do it to minimise memory use. You'll just need to stream the existing worksheets first before adding the data frames. Effectively you are always writing a new workbook anyway.
– Charlie Clark
Nov 26 '18 at 12:46
|
show 6 more comments
I am suffering from memory consumption issues when streaming data into an existing Excel template using OpenPyXl.
My process is as follows:
1. Open existing template
2. Stream data into 3 different sheets
3. Save the workbook and download to the user
Code below:
path = r'template.xlsx'
output = BytesIO()
# Saving to Excel
book = load_workbook(path)
days_ws = book['Days']
for r in dataframe_to_rows(days_df, index=True, header=True):
days_ws.append(r)
rates_ws = book['Daily Rates']
for r in dataframe_to_rows(rates_df, index=True, header=True):
rates_ws.append(r)
settings_ws = book['Settings']
for r in dataframe_to_rows(settings_df, index=True, header=True):
settings_ws.append(r)
with NamedTemporaryFile() as tmp:
book.save(tmp.name)
tmp.seek(0)
stream = tmp.read()
What I would like to achieve is the ability to open an existing spreadsheet in write_only mode, to allow for data streaming into the workbooks without huge memory overhead. I know this is possible when creating worksheets, however I have not been able to find an example using an existing one.
I would be happy to work with any libraries or modules outside of OpenPyXl if anyone has other suggestions. Cheers!
Thanks for your help in advance!
python excel openpyxl xlsxwriter
I am suffering from memory consumption issues when streaming data into an existing Excel template using OpenPyXl.
My process is as follows:
1. Open existing template
2. Stream data into 3 different sheets
3. Save the workbook and download to the user
Code below:
path = r'template.xlsx'
output = BytesIO()
# Saving to Excel
book = load_workbook(path)
days_ws = book['Days']
for r in dataframe_to_rows(days_df, index=True, header=True):
days_ws.append(r)
rates_ws = book['Daily Rates']
for r in dataframe_to_rows(rates_df, index=True, header=True):
rates_ws.append(r)
settings_ws = book['Settings']
for r in dataframe_to_rows(settings_df, index=True, header=True):
settings_ws.append(r)
with NamedTemporaryFile() as tmp:
book.save(tmp.name)
tmp.seek(0)
stream = tmp.read()
What I would like to achieve is the ability to open an existing spreadsheet in write_only mode, to allow for data streaming into the workbooks without huge memory overhead. I know this is possible when creating worksheets, however I have not been able to find an example using an existing one.
I would be happy to work with any libraries or modules outside of OpenPyXl if anyone has other suggestions. Cheers!
Thanks for your help in advance!
python excel openpyxl xlsxwriter
python excel openpyxl xlsxwriter
asked Nov 26 '18 at 10:11
Tom FarrandTom Farrand
11
11
You can combineread-only
andwrite-only
modes.
– Charlie Clark
Nov 26 '18 at 10:46
@CharlieClark thanks for your reply. I believe that is what is currently happening, and is the reason why my memory usage is so high. I am looking to reduce the memory consumption of the process. Currently it's ~600mb of memory to save a 2.3mb excel sheet.
– Tom Farrand
Nov 26 '18 at 11:48
No, it's not what you're currently doing.wb1 = load_workbook(path, read_only=True)
would be needed for that.wb2 = Workbook(write_only=True)
– Charlie Clark
Nov 26 '18 at 12:06
@CharlieClark if I understand correctly, that would open up my existing workbook in read only mode- preventing me from streaming data to it. Whilewb2 = Workbook(write_only=True)
would create a completely separate new workbook which I could then write data into.
– Tom Farrand
Nov 26 '18 at 12:36
That's the way to do it to minimise memory use. You'll just need to stream the existing worksheets first before adding the data frames. Effectively you are always writing a new workbook anyway.
– Charlie Clark
Nov 26 '18 at 12:46
|
show 6 more comments
You can combineread-only
andwrite-only
modes.
– Charlie Clark
Nov 26 '18 at 10:46
@CharlieClark thanks for your reply. I believe that is what is currently happening, and is the reason why my memory usage is so high. I am looking to reduce the memory consumption of the process. Currently it's ~600mb of memory to save a 2.3mb excel sheet.
– Tom Farrand
Nov 26 '18 at 11:48
No, it's not what you're currently doing.wb1 = load_workbook(path, read_only=True)
would be needed for that.wb2 = Workbook(write_only=True)
– Charlie Clark
Nov 26 '18 at 12:06
@CharlieClark if I understand correctly, that would open up my existing workbook in read only mode- preventing me from streaming data to it. Whilewb2 = Workbook(write_only=True)
would create a completely separate new workbook which I could then write data into.
– Tom Farrand
Nov 26 '18 at 12:36
That's the way to do it to minimise memory use. You'll just need to stream the existing worksheets first before adding the data frames. Effectively you are always writing a new workbook anyway.
– Charlie Clark
Nov 26 '18 at 12:46
You can combine
read-only
and write-only
modes.– Charlie Clark
Nov 26 '18 at 10:46
You can combine
read-only
and write-only
modes.– Charlie Clark
Nov 26 '18 at 10:46
@CharlieClark thanks for your reply. I believe that is what is currently happening, and is the reason why my memory usage is so high. I am looking to reduce the memory consumption of the process. Currently it's ~600mb of memory to save a 2.3mb excel sheet.
– Tom Farrand
Nov 26 '18 at 11:48
@CharlieClark thanks for your reply. I believe that is what is currently happening, and is the reason why my memory usage is so high. I am looking to reduce the memory consumption of the process. Currently it's ~600mb of memory to save a 2.3mb excel sheet.
– Tom Farrand
Nov 26 '18 at 11:48
No, it's not what you're currently doing.
wb1 = load_workbook(path, read_only=True)
would be needed for that. wb2 = Workbook(write_only=True)
– Charlie Clark
Nov 26 '18 at 12:06
No, it's not what you're currently doing.
wb1 = load_workbook(path, read_only=True)
would be needed for that. wb2 = Workbook(write_only=True)
– Charlie Clark
Nov 26 '18 at 12:06
@CharlieClark if I understand correctly, that would open up my existing workbook in read only mode- preventing me from streaming data to it. While
wb2 = Workbook(write_only=True)
would create a completely separate new workbook which I could then write data into.– Tom Farrand
Nov 26 '18 at 12:36
@CharlieClark if I understand correctly, that would open up my existing workbook in read only mode- preventing me from streaming data to it. While
wb2 = Workbook(write_only=True)
would create a completely separate new workbook which I could then write data into.– Tom Farrand
Nov 26 '18 at 12:36
That's the way to do it to minimise memory use. You'll just need to stream the existing worksheets first before adding the data frames. Effectively you are always writing a new workbook anyway.
– Charlie Clark
Nov 26 '18 at 12:46
That's the way to do it to minimise memory use. You'll just need to stream the existing worksheets first before adding the data frames. Effectively you are always writing a new workbook anyway.
– Charlie Clark
Nov 26 '18 at 12:46
|
show 6 more comments
0
active
oldest
votes
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',
autoActivateHeartbeat: false,
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
});
}
});
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%2f53478863%2fopenpyxl-open-an-existing-spreadsheet-in-write-only-mode%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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%2f53478863%2fopenpyxl-open-an-existing-spreadsheet-in-write-only-mode%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
You can combine
read-only
andwrite-only
modes.– Charlie Clark
Nov 26 '18 at 10:46
@CharlieClark thanks for your reply. I believe that is what is currently happening, and is the reason why my memory usage is so high. I am looking to reduce the memory consumption of the process. Currently it's ~600mb of memory to save a 2.3mb excel sheet.
– Tom Farrand
Nov 26 '18 at 11:48
No, it's not what you're currently doing.
wb1 = load_workbook(path, read_only=True)
would be needed for that.wb2 = Workbook(write_only=True)
– Charlie Clark
Nov 26 '18 at 12:06
@CharlieClark if I understand correctly, that would open up my existing workbook in read only mode- preventing me from streaming data to it. While
wb2 = Workbook(write_only=True)
would create a completely separate new workbook which I could then write data into.– Tom Farrand
Nov 26 '18 at 12:36
That's the way to do it to minimise memory use. You'll just need to stream the existing worksheets first before adding the data frames. Effectively you are always writing a new workbook anyway.
– Charlie Clark
Nov 26 '18 at 12:46