lundi 13 avril 2015

Why "delimiter" doesn't work when opening a text file by Excel by Python win32com


Vote count:

0




I need to open a text file into Excel, delimit the data by spaces. The text file sample is like:



3.00 1.1274526E+06 2.9389029E+02 4.1200144E+05 3.3034866E+03 1.1408163E+07 1.8452779E+04
4.00 1.4850185E+06 9.5322272E+02 1.1179841E+06 6.3805410E+03 3.9089748E+07 2.0955162E+04
5.00 1.8715192E+06 7.0729324E+01 1.9711849E+06 1.6726119E+03 7.9569296E+07 8.3463887E+03
6.00 1.6887904E+06 2.3728212E+02 2.3966372E+06 1.1016495E+03 1.1034450E+08 3.3214250E+03
7.00 1.8976470E+06 1.9970488E+02 3.3064400E+06 1.2176888E+03 1.1842174E+08 3.4934358E+03
8.00 1.6278459E+06 1.2140007E+02 3.5865918E+06 1.1238341E+03 1.4475696E+08 3.9887886E+03
9.00 1.1508129E+06 6.7652779E+01 3.1599262E+06 1.0420349E+03 2.3082283E+08 3.8466196E+03


In VBA, in order to delimite the data, I use



Sub opentext()
Workbooks.opentext Filename:=filepath, _
ConsecutiveDelimiter:=True, Space:=True, TextQualifier = xlTextQualifierNone
End Sub


The data are delimited like this: each data is in an independent cell, the result is perfect.


But if I do the same thing in python using win32com module by the following code:



import win32com.client
filepath = ......#the path of file
class ExcelOperator:
def __init__(self):
self.xlApp = win32com.client.Dispatch('Excel.Application')
self.xlApp.Workbooks.OpenText(filepath,DataType = 1 ,ConsecutiveDelimiter = True, Tab=True, Space = True, TextQualifier = 'xlTextQualifierNone')
eo = ExcelOperator()


The text file can be opened into Excel, but the delimiters don't work at all. The data is separated by lines and in each line there is only one cell containing all the data of the line. As I don't have enough reputation to post images, I have no screen captures in this question. Could anybody explain the reason of this bug? Is this the bug of COM interface?



asked 1 min ago







Why "delimiter" doesn't work when opening a text file by Excel by Python win32com

Aucun commentaire:

Enregistrer un commentaire