2013-09-30 1 views
1

저는 실제로 여기에 python win32com을 사용하여 Excel의 테두리를 포맷하는 코드 조각을 가지고 있습니다. 내 관심은 국경을 포맷하는 데 걸리는 시간입니다. 필자는 스크립트에서 매크로를 조 변경하는 데 필요한 정보를 찾기 위해 Excel에 매크로를 기록하려고 시도했지만 작동하지 않았습니다.python win32com excel border formatting

그래서 내가 할 수있는 최선의 방법은 항상 행 3에서 1 행 단위로 시작하고 1 행 단위로 1 행 단위로, 1 열 단위로 10 행 단위로 시작하는 범위 루프에서 실행하는 것입니다 거기에서 "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()"만 작동했습니다.

미리 감사드립니다.

답변

4

흠, 무엇을 사용하고 계십니까?

이 작동합니다 :

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 

이 당신을 위해 작동하는 방법을 알려주세요. 당신이 거짓으로 표시하거나을 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 

엑셀은 모든 통화에 대해 화면을 업데이트하지 않습니다 이쪽으로.