05.27 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()


分享到:


相關文章: