1 python excel border win32com
我在这里有一段代码,它实际上可以使用 python win32com 在 excel 中格式化边框。我担心的是格式化边框所需的时间。我试图在 excel 中记录一个宏,以找出在我的脚本中转置它所需的信息,但它没有用。
所以我能做的最好的事情是在 for range 循环中运行,我总是从第 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)
Run Code Online (Sandbox Code Playgroud)
我可以做些什么来格式化具有 ==> 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
Run Code Online (Sandbox Code Playgroud)
让我知道这对你有什么作用。
此外,如果您将 excel 应用程序 Visible 设置为 False 或关闭屏幕更新,速度可能会更快:
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
Run Code Online (Sandbox Code Playgroud)
这样,excel 不会为每次通话更新屏幕。
| 归档时间: |
|
| 查看次数: |
6014 次 |
| 最近记录: |