2015-10-04 181 views
1

這裏是我的代碼:導出數據到Excel

import xlwt 
file = 'C://Users/MM/Desktop/' 
wb = xlwt.Workbook() 
sh = wb.add_sheet('S1') 

d = [['Numbers','4,849,377','736,732','6,731,484']['Money','$43,253','$70,760','$49,774'],['Decnum','1.22','3.45','9.10']] 

for row, nums in enumerate(d): 
    for col, value in enumerate(nums): 
     sh.write(col, row , value) 

wb.save(file +'try.xls') 
print('Exported') 

以下是我想要完成的事情:

1)在第一行的一切需要大膽和斜體。

2)所有列的寬度需要相同(固定)。 3)「Numbers」,「Money」和「Decnum」在excel中給我一個錯誤,告訴我用綠色錯誤箭頭將它們轉換爲數字。

4)數據需要居中。

這裏是前景

<img>http://i59.tinypic.com/dgu48.jpg<img>

回答

2

您正在尋找replace()

>>> x = '1,2,3' 
>>> x.replace(',', '') 
'123' 

for row, nums in enumerate(d): 
    for col, value in enumerate(nums): 
     try: 
      value = int(value.replace(',', '')) 
     except: 
      pass 
     sh.write(col, row , value) 

樣式格式的例子:

style = xlwt.XFStyle() 

font = xlwt.Font() 
font.bold = True 
style.font = font 

sh.write(col, row , value, style=style) 

總結一下:

style = xlwt.XFStyle() 

font = xlwt.Font() 
font.bold = True 
style.font = font 

for row, nums in enumerate(d): 
    for col, value in enumerate(nums): 
     try: 
      value = int(value.replace(',', '')) 
     except: 
      pass 
     if col == 0: 
      sh.write(col, row , value, style=style) 
     else: 
      sh.write(col, row , value) 

另一種方法是使用easyxf()

head = xlwt.easyxf('align: horiz center; font: bold 1,') 
common = xlwt.easyxf('align: horiz center;') 

for row, nums in enumerate(d): 
    for col, value in enumerate(nums): 
     try: 
      value = int(value.replace(',', '')) 
     except: 
      pass 
     if col == 0: 
      sh.write(col, row, value, style=head) 
     else: 
      sh.write(col, row, value, style=common) 

寬度&高地

添加一個bedore wb.save()

import itertools 

col_width = 26 * 20 

try: 
    for i in itertools.count(): 
     sh.col(i).width = col_width 
except ValueError: 
    pass 

設置單元格格式

Additional formats

num = xlwt.easyxf('align: horiz center;', '#,##') 

if col == 0: 
    sh.write(col, row, value, style=head) 
else: 
    if type(value) is int: 
     sh.write(col, row, value, style=num) 
    else: 
     sh.write(col, row, value, style=common) 

全部片段:

import itertools 
import xlwt 

wb = xlwt.Workbook() 
sh = wb.add_sheet('S1') 

d = [['Numbers', '4,849,377', '736,732', '6,731,484'], 
    ['Letters', 'a', 'b', 'c'], ['Colors', 'red', 'green', 'yellow']] 


head = xlwt.easyxf('align: horiz center; font: bold 1,') 
common = xlwt.easyxf('align: horiz center;') 
num = xlwt.easyxf('align: horiz center;', '#,##') 

for row, nums in enumerate(d): 
    for col, value in enumerate(nums): 
     try: 
      value = int(value.replace(',', '')) 
     except: 
      pass 
     if col == 0: 
      sh.write(col, row, value, style=head) 
     else: 
      if type(value) is int: 
       sh.write(col, row, value, style=num) 
      else: 
       sh.write(col, row, value, style=common) 

    col_width = 200 * 20 

    try: 
     for i in itertools.count(): 
      sh.col(i).width = col_width 
    except ValueError: 
     pass 

wb.save('tryx.xls') 
print 'Exported' 
+0

好。我得到第一行有粗體文本。但是,我不想取代逗號。我需要保持他們。而且,斜體,列寬以及數據居中的情況如何?正如我所說,它應該看起來像圖像中的數據。 – user5403501

+0

@ user5403501更新了對齊方式 –

+0

@ user3990145:除了擴大列外,我得到了一切。我使用了代碼的第二種方法;與easyxf的一個。你能告訴我如何擴大列到一定的寬度? – user5403501