如何在xlwt中编写多个列的单元格? [英] How to write a cell with multiple columns in xlwt?
问题描述
我想写一张这样的表:
------------ ----
|长细胞|
----------------
| 1 | 2 |
----------------
如何写单元格长单元格
?谢谢。
我试图这样做:
sheet.write(0,0,'Long Cell')
sheet.write(1,0,1)
sheet.write(1,1,2)
但最终如下:
--------------------
|长细胞| |
--------------------
| 1 | 2 |
--------------------
据我所知,这是没有记录的 - 你必须阅读源代码才能找到它。在 Worksheet
类中有两种方法来执行此操作, write_merge
和 merge
。 merge
将现有的单元格合并,并将它们合并,而 write_merge
写入一个标签(就像 / code>),然后执行相同的东西
merge
。
都将单元格合并作为 r1,r2,c1,c2
,并接受可选的样式
参数。
从您的示例中,这将是最简单的调用:
sheet.write_merge(0,0, 0,1,'Long Cell')
pre>
sheet.write(1,0,1)
sheet.write(1,1,2)
更明确地说明如何工作:
top_row = 0
bottom_row = 0
left_column = 0
right_column = 1
sheet.write_merge(top_row,bottom_row,left_column,right_column,'Long Cell')
或者,使用
merge
:sheet.write(top_row,left_column,'Long Cell')
sheet.merge(top_row,bottom_row,left_column,right_colu mn)
merge
来源指出潜在的问题:
#问题:(1)要使用的风格应该存在风格的
#左上角的单元格,而不是一个参数。
#(2)应确保
#非左上角单元格中的任何先前的数据值都不为零。
#注意:如果一个单元格是由数据记录设置的,那么以后
#由[MUL] BLANK记录引用,Excel将空白
#输出屏幕上的单元格,但是OOo &安培; Gnu不会
#空白它。需要做的更好,比写
#多个记录。在此期间,避免使用此方法并使用
#write_merge()替代。
但是这样一个简单的例子可能会很好。 / p>
I'd like to write a table like this:
---------------- | Long Cell | ---------------- | 1 | 2 | ----------------
How to write the cell
Long Cell
? Thanks.I've tried to do it like this:
sheet.write(0, 0, 'Long Cell') sheet.write(1, 0, 1) sheet.write(1, 1, 2)
But it end up like:
-------------------- | Long Cell | | -------------------- | 1 | 2 | --------------------
解决方案As far as I can tell, this isn't documented - you have to read the source code to find it. There are two methods on the
Worksheet
class to do this,write_merge
andmerge
.merge
takes existing cells and merges them, whilewrite_merge
writes a label (just likewrite
) and then does the same stuffmerge
does.Both take the cells to merge as
r1, r2, c1, c2
, and accept an optionalstyle
parameter.From your example, this would be the simplest call:
sheet.write_merge(0, 0, 0, 1, 'Long Cell') sheet.write(1, 0, 1) sheet.write(1, 1, 2)
To be more explicit about how the call works:
top_row = 0 bottom_row = 0 left_column = 0 right_column = 1 sheet.write_merge(top_row, bottom_row, left_column, right_column, 'Long Cell')
Alternatively, using
merge
:sheet.write(top_row, left_column, 'Long Cell') sheet.merge(top_row, bottom_row, left_column, right_column)
merge
has some comments in the source pointing out potential problems:# Problems: (1) style to be used should be existing style of # the top-left cell, not an arg. # (2) should ensure that any previous data value in # non-top-left cells is nobbled. # Note: if a cell is set by a data record then later # is referenced by a [MUL]BLANK record, Excel will blank # out the cell on the screen, but OOo & Gnu will not # blank it out. Need to do something better than writing # multiple records. In the meantime, avoid this method and use # write_merge() instead.
But it would be fine for a simple case like this.
这篇关于如何在xlwt中编写多个列的单元格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!