python-excel轉html

說明

已知一份excel文件

OleView.exe 查看excel支持的接口

只支持在windows機器上運行

方案

具體思路就是將excel文件打開,然後使用“另存為”功能,將其轉換為html文檔。需要用到 win32com 模塊提供的功能,使用的是win com 接口編程。不需要熟悉COM,只需要知道操作的技術。

1、找到使用的接口

使用OleView可以查看當前系統所有組件支持的COM接口。

A、打開OleView,並選中Type Libraries

python-excel轉html

B、找到 Excel,並雙擊打開,複製右側文本到其他編輯器查找需要的函數

python-excel轉html

python-excel轉html

C、比如查找的 SaveAs接口

python-excel轉html

2、使用Python操作

import win32com.client

import os

if__name__=='__main__':

excel=win32com.client.Dispatch('Excel.Application')

file_name="2017-12-18.xlsx"

file_name=os.path.abspath(file_name)

workbook=excel.Workbooks.Open(file_name)

workbook.SaveAs(Filename="test.html",FileFormat=win32com.client.constants.xlHtml)

workbook.Close()

excel.Quit()


分享到:


相關文章: