Hi Thomas, welcome to the group.
From almost exactly a year ago (2008-05-06) Harald Armin Massa was
wondering the same thing:
=== start of extract from the archives ===
Harald Armin Massa wrote:
> > hello,
> >
> > I am triyng to write out text into an excel-worksheet.
> >
> > w = xlwt.Workbook()
> > ws=w.add_sheet(sheetname)
> >
> > ws.write(20,20,"this is line1 \n this is line 2 \n this is line 3)
Thomas, that's what a simplified program looks like :-)
> > when opening the file in excel, there IS some symbol indicating a line
> > break, and after F2 and return on every cell, it gets formated
> > correctly...
> >
> > but as the table has around 2000 cells with this kind of data,
> > pressing 2000 times F2 + return is not really an option :)
> >
> > so: how can I put a line break into the data in one cell?
> >
Welcome, Harald.
This problem had me stumped for a while. Using Excel 2003, type
foo\nbar\nzot (where \n is effected by Alt-Enter), save the file,
inspect it with xlrd, sure enough the cell contains 'foo\nbar\nzot', use
xlwt to write that, open the new file with Excel: same problem as you
noticed. Whether Excel displays a symbol or not may depend on which
version and which "service pack" you have.
Now try this:
Select an empty cell in Excel, do Format > Cells > Alignment, notice
nothing unusual, change nothing, exit, type foo\nbar\nzot (where \n is
effected by Alt-Enter) then Enter, then do Format > Cells > Alignment,
notice that the "Wrap" box is now ticked!
So, short answer to your question: use a style (aka XF) with the
Alignment wrap set to 1.
import xlwt
algn1 = xlwt.Alignment()
algn1.wrap = 1
style1 = xlwt.XFStyle()
style1.alignment = algn1
wb = xlwt.Workbook()
ws = wb.add_sheet('LineBreakDemo')
ws.write(10, 0, 'Hard\nLine\nBreak', style1)
ws.write(10, 1, 'Bad\nLine\nBreak')
ws.write(10, 2, 'Soft Line Break', style1)
ws.write(10, 3, 'No Line Break')
wb.save('LineBreakDemo.xls')
=== end of extract from the archives ===
Nowadays, we'd use easyxf instead to create style1:
style1 = xlwt.easyxf("alignment: wrap on") # plus whatever other things
you need
HTH,
John
weird * 3
>
> I'm using Excel 2003, maybe this is a bug or maybe I have to set other
> properties together with wrap?
or maybe the planets are not aligned ... speculation is futile.
If you like I can send you the result,
send result PLUS the code that created it
> unfortunately I'm not very familiar with Excel, I only need to write
> an exporter for our data!
Cheers,
John
And they are also display as expected by:
* English Excel 2003 (11.8302.82210 SP3)
* English Excel 2007
* Excel Viewer
Forgive me if reviving an old post is not the preferred method within this group, but the post below outlines the basic problem I'm facing.
I have a column within my worksheet which has data in the following format:"Problem:\nSome Problem Text Here.\nSolution:\nSolution to the Problem Here\n\n"
I want the \n to be observed as the "newline"|Alt+Enter character, and it just is not. I continue to see the line printed as above rather than observing the newlines.
I have experimented with the wrap on/off and this makes no difference. I have also experimented with both XFStyle AND easyxf- neither of which makes a difference.Is there some other method by which I can enforce the newline character within a cell or column?
On Wednesday, July 25, 2012 12:53:09 PM UTC+10, grap3_ap3 wrote:Forgive me if reviving an old post is not the preferred method within this group, but the post below outlines the basic problem I'm facing.
I would rather rely on YOU outlining what your problem is, together with the minimal Python code that demonstrates the problem -- in this case, about 10 lines.
I have a column within my worksheet which has data in the following format:"Problem:\nSome Problem Text Here.\nSolution:\nSolution to the Problem Here\n\n"
Column?? Do you mean "cell"? Please show the Python code.
I want the \n to be observed as the "newline"|Alt+Enter character, and it just is not. I continue to see the line printed as above rather than observing the newlines.
Printed? By what software on what device? If you mean that you can actually see the backslash and the n when looking at the Excel screen, then you must have doubled up your backslashes in your script somehow e.g. "foo\\nbar" or r"foo\nbar" instead of "foo\nbar"
I have experimented with the wrap on/off and this makes no difference. I have also experimented with both XFStyle AND easyxf- neither of which makes a difference.Is there some other method by which I can enforce the newline character within a cell or column?
There is no method other than as outlined in the earlier posts.