当前位置: 首页 > 图灵资讯 > 行业资讯> python如何清空excel单元格

python如何清空excel单元格

来源:图灵python
时间: 2026-04-28 15:50:50

在python中,单元格可以通过给Excel赋空值来清空。

示例:

单元格如下:

清空单元格代码如下:

importos
importwin32comm
fromwin32comm.clientimportconstantsasc#目的是直接使用VBA常数
current_address=os.path.abspath('.')

excel_address=os.path.join(current_address,"示例.xlsx")
print(current_address)
xl_app=win32com.client.gencache.EnsureDispatch("Excel.Application")#如果您想引用常数,请使用此方法调用Excel
xl_app.Visible=False#Excel文件是否显示
wb=xl_app.Workbooks.Open(excel_address)
sht=wb.Worksheets(1)
sht.Name="示例"

rng_1=sht.Range(sht.Cells(2,1),sht.Cells(2,3))
rng_1.Value=""
rng_1.Interior.Pattern=c.xlNone
rng_1.Interior.TintAndShade=0
rng_1.Interior.PatternTintAndShade=0

wb.Save()
wb.Close()

执行结果:

请关注Python视频教程栏目,了解更多Python知识。

上一篇:

python如何求次幂?

下一篇:

返回列表