question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

merge_range works only one time in constant memory mode

See original GitHub issue

Hi everyone,

im using Python 3.4.3 , xlsxwriter 0.7.3

here a short code example:

workbook = xlsxwriter.Workbook("text.xlsx", {'constant_memory': True})

worksheet = workbook.add_worksheet()

header = workbook.add_format({'bold': True, 'border': 1,
                              'border_color': "#000000",
                              'bg_color': '#808080',
                              'valign': "vcenter",
                              'align': "center",
                              'font_color': '#FFFFFF'})

header.set_text_wrap()

worksheet.merge_range(5,0,6,0, 'Ebene', header)
worksheet.merge_range(5,1,6,1, 'Monat', header)
worksheet.merge_range(5,2,6,2, 'Marke', header)
worksheet.merge_range(5,3,6,3, 'abcde', header)

workbook.close()

and the result shown in Excel 2013 weird_merge_range

no matter how i reorder it, always the first call of merge_range works properly, the following ones are broken like above.

Remove the {'constant_memory': True} and everything works fine.

regards

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
jmcnamaracommented, Sep 27, 2015

Hi,

I’ve added documentation to highlight that merged ranges don’t work when constant_memory mode is enabled.

It is possible to workaround this limitation by writing the merge range cells in row-column order and later calling merge_range(). This requires filling in any non data cells in the range with blank cells. Here is an example based on yours:


import xlsxwriter

workbook = xlsxwriter.Workbook("text2.xlsx", {'constant_memory': True})

worksheet = workbook.add_worksheet()

header = workbook.add_format({'bold': True, 'border': 1,
                              'border_color': "#000000",
                              'bg_color': '#808080',
                              'valign': "vcenter",
                              'align': "center",
                              'font_color': '#FFFFFF'})

header.set_text_wrap()

# Write the merged range cells in row column order so that they conform to the
# rule for 'constant_memory' mode. Fill in any non-text cells with blank
# formatted cells.
worksheet.write(5, 0, 'Ebene', header)
worksheet.write(5, 1, 'Monat', header)
worksheet.write(5, 2, 'Marke', header)
worksheet.write(5, 3, 'abcde', header)

worksheet.write_blank(6, 0, None, header)
worksheet.write_blank(6, 1, None, header)
worksheet.write_blank(6, 2, None, header)
worksheet.write_blank(6, 3, None, header)

# Some other text.
worksheet.write(8, 0, 'Later')

# Right the merge ranges at the end.
worksheet.merge_range(5, 0, 6, 0, 'Ebene', header)
worksheet.merge_range(5, 1, 6, 1, 'Monat', header)
worksheet.merge_range(5, 2, 6, 2, 'Marke', header)
worksheet.merge_range(5, 3, 6, 3, 'abcde', header)

workbook.close()

To fix this properly would require a reasonable amount of work that probably isn’t worth it for the small number of people that this would affect.

So I’m closing this as a documented limitation.

Regards,

John

0reactions
yunfancommented, Oct 10, 2019

@jmcnamara thanks for you time. so the problem is you need to feed the writer iterated, from my view, its like the common issue among SQL ORM libraries. many has a autocommit option which let the user’s operation be send to database, but you still could hold some for your purpose and commit explicit

or you could treat writer job like a transaction in those SQL operations. mostly its single statement, while sometime it could accept multi statement. in this case, mostly you could send single row, but sometimes you could send multi rows with merge operation done

Read more comments on GitHub >

github_iconTop Results From Across the Web

Working with Memory and Performance - XlsxWriter
XlsxWriter in constant_memory mode: the execution time still increases linearly with the number of rows but the memory usage remains small and constant: ......
Read more >
Python Xlsxwriter not overwriting when in constant mode on
From the XslxWriter docs: constant_memory: Reduces the amount of data stored in memory so that large files can be written efficiently:
Read more >
Working with Memory and Performance - libxlsxwriter
Constant Memory Mode. By default libxlsxwriter holds all cell data in memory to allow non-sequential data storage. The effect of this is that...
Read more >
VBA Merge Cells & Unmerge Cells
You can merge columns using VBA and the column letters. The following code will merge columns A:C. Sub MergeColumns() Range("A:C").Merge End Sub.
Read more >
ALTER PARTITION FUNCTION (Transact-SQL) - SQL Server
The statement can also merge two partitions into one partition. Caution. More than one table or index can use the same partition function....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found