vba 如何在 Python 中将 Excel 工作表另存为 HTML?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19631511/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 00:05:50  来源:igfitidea点击:

How do I save Excel Sheet as HTML in Python?

pythonhtmlexcelvbaexcel-vba

提问by ComputerFellow

I'm working with this library XlsxWriter.

我正在使用这个库XlsxWriter

I've opened a workbook and written some stuff in it (considering the official example) -

我打开了一本工作簿并在其中写了一些东西(考虑到官方示例)-

import xlsxwriter

# Create a workbook and add a worksheet.
workbook = xlsxwriter.Workbook('Expenses01.xlsx')
worksheet = workbook.add_worksheet()

# Some data we want to write to the worksheet.
expenses = (
    ['Rent', 1000],
    ['Gas',   100],
    ['Food',  300],
    ['Gym',    50],
)

# Start from the first cell. Rows and columns are zero indexed.
row = 0
col = 0

# Iterate over the data and write it out row by row.
for item, cost in (expenses):
    worksheet.write(row, col,     item)
    worksheet.write(row, col + 1, cost)
    row += 1

# Write a total using a formula.
worksheet.write(row, 0, 'Total')
worksheet.write(row, 1, '=SUM(B1:B4)')

workbook.close()

I've gone through the docs rigorously but can't seem to find the save asfunctionality.

我已经严格浏览了文档,但似乎无法找到另存为功能。

Is there a way (any way) to save the workbookas a HTMLfile?

有没有办法(任何方式)将其保存workbookHTML文件?

If it isn't possible from python code, can I somehow write VBAcode and call that code from python?

如果从 python 代码中不可能,我可以以某种方式编写VBA代码并从 python 中调用该代码吗?

回答by Jerome Montino

You can use win32com.clientto call a VBA macro. Assuming your file is named Bar...

您可以使用win32com.client来调用 VBA 宏。假设您的文件名为 Bar ...

VBA:

VBA:

Sub SaveHTML()
ThisWorkbook.SaveAs Filename:="C:\Foo\Bar.htm", FileFormat:=xlHtml
End Sub

Python:

Python:

from win32com.client import Dispatch

xl = Dispatch('Excel.Application')
xl.Workbooks.Open('C:\Foo\Bar.xlsx')
#xl.Visible = True -- optional
xl.Application.Run("SaveHTML")
xl.Workbooks.Close

Modify as necessary.

根据需要进行修改。

EDIT: I forgot to add, using win32com to close Excel is an absolute pain. The workbooks will close, but the application itself will linger (check Task Manager). Please refer to this SO poston a workaround for this.

编辑:我忘了补充,使用 win32com 关闭 Excel 是绝对的痛苦。工作簿将关闭,但应用程序本身将停留(检查任务管理器)。请参阅此 SO 帖子以解决此问题。

回答by nrhorner

Maybe you could use xlrd to parse your excel files before converting to html http://codingtutorials.co.uk/python-excel-xlrd-xlwt/

也许您可以在转换为 html 之前使用 xlrd 解析您的 excel 文件http://codingtutorials.co.uk/python-excel-xlrd-xlwt/

回答by Oliver

You can save to HTML from Python directly then attach to email:

您可以直接从 Python 保存到 HTML,然后附加到电子邮件:

from win32com.client.gencache import EnsureDispatch
from win32com.client import constants

yourExcelFile = ...
newFileName = ...

xl = EnsureDispatch('Excel.Application')
wb = xl.Workbooks.Open(yourExcelFile)
wb.SaveAs(newFileName, constants.xlHtml)
xl.Workbooks.Close()
xl.Quit()
del xl