用於導出Excel 2007+ .xlsx文件的更現代的替代方法是xlsxcessive。以下是一個使標題行稍大且粗體的示例。這真的不是太難,而且記錄得很好。
from xlsxcessive.xlsx import Workbook, save
#Create some fake test data
import urllib, random
words = [x.strip().title() for x in urllib.urlopen('http://dictionary-thesaurus.com/wordlists/Nouns%285,449%29.txt').readlines()]
names = []
for i in range(25):
address = '%s %s %s' % (random.randint(0, 100000), random.choice(words), random.choice(['Ave', 'St', 'Blvd', 'Ct', 'Ln']))
names.append((random.choice(words), random.choice(words), address))
#Create the workbook and sheet
wb = Workbook()
s1 = wb.new_sheet('Sheet 1')
#Create the bold style for the header row
headerfmt = wb.stylesheet.new_format()
headerfmt.font(size=14, bold=True)
#Write out the header row
header = ['Infant Name','Mother Name','Mother Address',]
for col, label in enumerate(header):
s1.cell(coords=(0,col), value=label, format=headerfmt)
#Write out the data
for rownumber, rowvalues in enumerate(names, start=1):
for col, value in enumerate(rowvalues):
s1.cell(coords=(rownumber, col), value=value)
save(wb, 'stackoverflow_problem.xlsx')
是什麼讓您認爲除了CSV文件中的數據之外還可以提供裝飾?你在哪裏見過這件事? – 2010-09-20 14:09:44