2013-10-30 21 views
18

Böyle bir tablo yazmak istiyorum:xlwt'de birden çok sütun içeren bir hücre nasıl yazılır?

---------------- 
| Long Cell | 
---------------- 
| 1 | 2  | 
---------------- 

Nasıl hücreyi Long Cell yazmak için? Teşekkürler.

sheet.write(0, 0, 'Long Cell') 
sheet.write(1, 0, 1) 
sheet.write(1, 1, 2) 

Ama sonunun böyle:

Ben böyle yapmaya çalıştık Bildiğim kadarıyla, bu belgelenmiş değildir söyleyebilirim

-------------------- 
| Long Cell |  | 
-------------------- 
| 1   | 2 | 
-------------------- 
+0

@ShivanRaptor Sorumu güncelledim. Teşekkürler. –

cevap

42

- Mecbur bulmak için kaynak kodunu okuyun. Bunu yapmak için Worksheet sınıfında iki yöntem vardır, write_merge ve merge. merge varolan hücreleri alır ve onları birleştirir, write_merge bir etiket (write gibi) yazıyor ve daha sonra merge aynı şeyler yapar.

Her iki hücreyi de r1, r2, c1, c2 olarak birleştirin ve isteğe bağlı bir style parametresini kabul edin.

sheet.write_merge(0, 0, 0, 1, 'Long Cell') 
sheet.write(1, 0, 1) 
sheet.write(1, 1, 2) 

çağrı nasıl çalıştığı konusunda daha açık olmak gerekirse: Alternatif

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 kullanarak:

sheet.write(top_row, left_column, 'Long Cell') 
sheet.merge(top_row, bottom_row, left_column, right_column) 
sizin örnekten

, bu en basit çağrı olurdu

merge kaynağında işaret eden bazı yorumlar vardır ntial sorunlar:

# 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. 

Ama böyle basit bir durum için iyi olurdu.

+1

Bu harika! Teşekkürler. –

İlgili konular