xlwt
http://pypi.python.org/pypi/xlrd
簡單使用
導入xlwt
import xlwt
新建一個excel文件
file = xlwt.Workbook() #注意這裏的Workbook首字母是大寫,無語吧
新建一個sheet
table = file.add_sheet('sheet name')
寫入數據table.write(行,列,value)
table.write(0,0,'test')
如果對一個單元格重複操作,會引發
returns error: # Exception: Attempt to overwrite cell: # sheetname=u'sheet 1' rowx=0 colx=0所以在打開時加cell_overwrite_ok=True解決
table = file.add_sheet('sheet name',cell_overwrite_ok=True)
保存文件
file.save('demo.xls')
另外,使用style
style = xlwt.XFStyle() #初始化樣式
font = xlwt.Font() #爲樣式創建字體
font.name = 'Times New Roman'
font.bold = True
style.font = font #爲樣式設置字體
table.write(0, 0, 'some bold Times text', style) # 使用樣式
xlwt 允許單元格或者整行地設置格式。還可以添加鏈接以及公式。可以閱讀源代碼,那裏有例子:
dates.py, 展示如何設置不同的數據格式
hyperlinks.py, 展示如何創建超鏈接 (hint: you need to use a formula)
merged.py, 展示如何合併格子
row_styles.py, 展示如何應用Style到整行格子中.
具體的例子可以看:
http://scienceoss.com/write-excel-files-with-python-using-xlwt/
google論壇:
http://groups.google.com/group/python-excel/
摘自:
xlwt
http://pypi.python.org/pypi/xlrd
簡單使用
導入xlwt
import xlwt
新建一個excel文件
file = xlwt.Workbook() #注意這裏的Workbook首字母是大寫,無語吧
新建一個sheet
table = file.add_sheet('sheet name')
寫入數據table.write(行,列,value)
table.write(0,0,'test')
如果對一個單元格重複操作,會引發
returns error: # Exception: Attempt to overwrite cell: # sheetname=u'sheet 1' rowx=0 colx=0所以在打開時加cell_overwrite_ok=True解決
table = file.add_sheet('sheet name',cell_overwrite_ok=True)
保存文件
file.save('demo.xls')
另外,使用style
style = xlwt.XFStyle() #初始化樣式
font = xlwt.Font() #爲樣式創建字體
font.name = 'Times New Roman'
font.bold = True
style.font = font #爲樣式設置字體
table.write(0, 0, 'some bold Times text', style) # 使用樣式
xlwt 允許單元格或者整行地設置格式。還可以添加鏈接以及公式。可以閱讀源代碼,那裏有例子:
dates.py, 展示如何設置不同的數據格式
hyperlinks.py, 展示如何創建超鏈接 (hint: you need to use a formula)
merged.py, 展示如何合併格子
row_styles.py, 展示如何應用Style到整行格子中.
具體的例子可以看:
http://scienceoss.com/write-excel-files-with-python-using-xlwt/
google論壇:
http://groups.google.com/group/python-excel/