如何在Python中使用win32com切片Excel数据(3.6)

时间:2017-07-07 07:28:42

标签: python excel win32com

我想在Excel中分割数据。

这是我的代码

import win32com.client

excel = win32com.client.Dispatch("Excel.Application")
excel.Visible = True
wb = excel.Workbooks.Open('C:\\Users\\a\\Desktop\\bin256.xlsx')
ws = wb.ActiveSheet
input_number = ws.Range('U4:U15363')
number = [input_number[i:i+60] for i in range(0,len(input_number),60)]

但是我收到了这个错误:

Traceback (most recent call last):
  File "<pyshell#13>", line 1, in <module>
    number = [input_number[i:i+60] for i in range(0,len(input_number),60)]
  File "<pyshell#13>", line 1, in <listcomp>
    number = [input_number[i:i+60] for i in range(0,len(input_number),60)]
  File "C:\Users\a\AppData\Local\Programs\Python\Python36-32\lib\site-packages\win32com\client\dynamic.py", line 256, in __getitem__
    return self._get_good_object_(self._oleobj_.Invoke(dispid, LCID, invkind, 1, index))
TypeError: Objects of type 'slice' can not be converted to a COM VARIANT

我该如何解决?提前谢谢。

1 个答案:

答案 0 :(得分:0)

您是否只想打开Excel? 也许你可以试试熊猫

import pandas as pd
file_path=r'E:\download\tmp\test.xlsx'
file_path='./tmp/test.xlsx'
df_name.to_excel(file_path,sheet_name='Sheet1',index=False)