2017-07-07 48 views
0

我在Excel中有要分割的數據。如何在Python中使用win32com分割Excel數據(3.6)

這裏是我的代碼

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 

我該如何解決呢?提前致謝。

回答

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) 
+0

沒有我打開Excel和讀取位於U(行)數據 – seokbin

+0

我想分割數據,每60 – seokbin