[英]python win32com excel border formatting
我這里有一段代碼,實際上是使用python win32com來格式化excel中的邊框。 我擔心的是格式化邊界所需的時間。 我試圖在excel中記錄一個宏,以找出在我的腳本中轉置它所需的信息,但它不起作用。
所以我能做的最好的事情是在一個范圍循環中運行,我總是從第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()”有效。
提前致謝。
嗯,你用的是什么擅長的?
這應該工作:
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應用程序Visible設置為False或關閉screenupdating,它可能會更快:
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不會為每次通話更新屏幕。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.