pythonwin32com excel边框格式

我在这里有一段代码,实际上是使用python win32com在Excel中格式化边框。 我关心的是格式化边界的时间。 我试图在excel中logging一个macros来找出所需的信息来转置它在我的脚本,但它没有工作。

所以我能做的最好的是运行在一个范围循环,我总是从第3行开始,直到一个叫做shn [1]的行计数器,增量为1,第1列为10,增量为1.从那里我使用“BorderAround()”工作正常,但太慢。 在这里我的一段代码:

for shn in [("Beam-Beam", bb_row, bb_col), ("Beam-Col", bc_row, bc_col)]: sheet = book.Worksheets(shn[0]) sheet.Range( "J3:DW3" ).Copy() if shn[0] == "Beam-Col": sheet.Range( "J3:AA3" ).Copy() sheet.Range( sheet.Cells( 4, 10 ), sheet.Cells( shn[1]-1, 10 ) ).PasteSpecial() for mrow in xrange(3,shn[1],1): for mcol in xrange(1,10,1): sheet.Cells(mrow, mcol).BorderAround()#.Border(1) 

有什么我可以做的格式的范围,如==> sheet.Range(sheet.Cells(3,1),sheet.Cells(shn [1],10))? 我试过“.Borders(11)”和“.Borders(12)”加“.BorderAround()”,但只有“.BorderAround()”已经工作。

提前致谢。

嗯,你用什么excel?

这应该工作:

 for shn in [("Beam-Beam", bb_row, bb_col), ("Beam-Col", bc_row, bc_col)]: sheet = book.Worksheets(shn[0]) sheet.Range( "J3:DW3" ).Copy() if shn[0] == "Beam-Col": sheet.Range( "J3:AA3" ).Copy() ## Set a variable named rng to the range rng = sheet.Range( sheet.Cells( 4, 10 ), sheet.Cells( shn[1]-1, 10 ) ) rng.PasteSpecial() ## Using this range, we can now set its borders linestyle and weight ## -> where 7 through 13 correspond to borders for xlEdgeTop,xlEdgeBottom, ## xlEdgeRight, xlEdgeLeft, xlInsideHorizontal, and xlInsideVertical ## -> LineStyle of 1 = xlContinous ## -> Weight of 2 = xlThin for border_id in xrange(7,13): rng.Borders(border_id).LineStyle=1 rng.Borders(border_id).Weight=2 ## And to finish just call book.Close(True) # To close book and save excel_app.Quit() # or some variable name established for the com instance 

让我知道这是如何工作的。

如果您将Excel应用程序设置为“可见”或“closures”屏幕更新,则可能会更快。

 excel_app.Visible = False # This will not physically open the book excel_app.ScreenUpdating = False # This will not update the screen on an open book ## # Do Stuff... ## # Just make sure when using the ScreenUpdating feature that you reenable it before closing excel_app.ScreenUpdating = True 

这种方式Excel不会更新每个电话的屏幕。