打开excel如上篇
import win32com
import win32com.client
xlApp = win32com.client.Dispatch('Excel.Application')
xlApp.Visible=1
fullPath=RUNINPUT+'/t1.xlsm'
try:
xlBook = xlApp.Workbooks.Open(fullPath, ReadOnly = False)
sht = xlBook.Worksheets('test')
except Exception:
xlApp.Quit()
1. 写入时间
sht.Cells(rorNum,colNum).Formula=time.strftime("%d/%m/%Y", time.localtime())
2. 设置打印区域
sht.PageSetup.PrintArea='$A$1:$C$'+str(excelRowNum)
3.设置插入图片的属性,更改为大小和位置随单元格而变。
插入的图片默认的属性是:大小不变, 位置随单元格而变。
这样有个问题是,如果使用了筛选功能,图片所在的单元格需要隐藏时,图片不能正确隐藏(会显示在最后一行)。
为了使图片随着单元格的隐藏而隐藏,则需要更改为“大小和位置随单元格而变。”
代码如下:
shape1=sht.Shapes.AddPicture(filePath1, False,True,left,top,width1,height1)
shape1.Placement=1