[英]Exporting the data to Excel
Here is my code: 这是我的代码:
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')
Following are the things I'm trying to accomplish: 以下是我要完成的工作:
1) Everything in first row needs to be bold and in italics. 1)第一行中的所有内容都必须为粗体和斜体。
2) The width for all the columns needs to be same(fixed). 2)所有列的宽度必须相同(固定)。
3) The 'Numbers','Money', and 'Decnum' gives me an error in excel telling me to convert them into numbers with green error arrows. 3)“数字”,“钱”和“小数”在excel中给我一个错误,告诉我将其转换为带有绿色错误箭头的数字。
4) The data needs to centered. 4)数据需要居中。
Here is an example of the outlook 这是前景的一个例子
You are looking for replace()
您正在寻找
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 formatting: 样式格式:
style = xlwt.XFStyle()
font = xlwt.Font()
font.bold = True
style.font = font
sh.write(col, row , value, style=style)
Summarize: 总结:
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)
Another approach is to use easyxf()
: 另一种方法是使用
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)
Widths & Heights 宽度和高度
Add that bedore wb.save()
添加那个bedore
wb.save()
import itertools
col_width = 26 * 20
try:
for i in itertools.count():
sh.col(i).width = col_width
except ValueError:
pass
Setting cell format 设定储存格格式
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)
Full snippet: 完整摘要:
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'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.