@xw.sub
def excel_cut():
gr = xw.Book.caller()
# 在A30处输入你想要截取的行数
n = int(gr.sheets('Sheet1').range('A30').value)
app = xw.App(visible=False, add_book=False)
wb = app.books.add()
sheet1 = wb.sheets["sheet1"]
sheet1.range('A1').value = gr.sheets['Sheet1'][:n, :1000].value
sheet1.range('A1').column_width = 13
A = sheet1.range('A1:A1000')
B = sheet1.range('B1:ZZ1')
A.api.Font.ColorIndex = 33
A.api.Font.Bold = True
B.api.Font.Bold = True
B.api.Font.ColorIndex = 3
wb.save('C:/Users/86155/Desktop/excel_cut.xlsm')
wb.close()
app.quit()
1.先在开发工具中点击此选项
2.进入此界面后点击工具中的引用
3.在此界面勾选xlwings并点击确定