2013-10-27 25 views
0

HTML我通過了xlwt,xlrd這些鏈接等各個環節和xlutils writing to existing workbook using xlwt保存現有工作表中的蟒蛇

from xlutils.copy import copy  
from xlrd import open_workbook  
from xlwt import easyxf  
rb = open_workbook('example.xls',formatting_info=True)  
r_sheet = rb.sheet_by_index(0)  
print r_sheet  
print r_sheet.name  
wb = copy(rb)  
w_sheet = wb.get_sheet(0)  
print w_sheet.name  
w_sheet.save('example.html') #it throws an error in the last line saying "AttributeError: 'Worksheet' object has no attribute 'save'" 

我如何可以單獨保存工作表作爲HTML文件uing蟒蛇去

+0

您正在嘗試使用xlrd Book類實例*保存*。從[docs](https://secure.simplistix.co.uk/svn/xlrd/trunk/xlrd/doc/xlrd.html?p=4966)中的xlrd描述:'''此包用於讀取數據並從Excel文件格式化信息。 Book類沒有保存方法。 – wwii

回答

1

我不確定你可以使用xlwt或xlrd將表格保存爲html格式。

一種替代方法是使用內部使用xlrd和xlwt的pandas,爲您節省編碼所涉及的所有步驟。

您可以

df = pandas.read_excel('example.xls', sheetname='sheet1') 

閱讀您的Excel工作表,並把它與HTML:

html = df.to_html() 

HTML是一個字符串,你可以在一個文件中保存open(myfile, 'w').write(html)