python win32com excel边框格式

2024-10-03 21:28:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我这里有一段代码,它实际上可以使用pythonwin32com在excel中格式化边框。我关心的是制定边界的时间。我试图在excel中记录一个宏,以找出将其转换到脚本中所需的信息,但没有成功。在

所以我能做的最好的就是在for range循环中运行,我总是从第3行开始,到一个名为shn[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)

我能做些什么来设置边框的格式,比如==>;工作表范围( 工作表单元格(3,1),工作表单元格(shn[1],10))?我尝试了“.Borders(11)”和“.Borders(12)”以及“.borderabout()”,但只有“.borderAbound()”起作用。在

提前谢谢。在


Tags: 代码inforrangecolexcelsheet边框
1条回答
网友
1楼 · 发布于 2024-10-03 21:28:20

嗯,你在用什么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应用程序设置为“可见”或关闭屏幕更新,则速度可能更快:

^{pr2}$

这样,excel就不会为每次呼叫更新屏幕。在

相关问题 更多 >