pywin32 - saving excel file as tab-delimited text file without quotes -
pywin32 - saving excel file as tab-delimited text file without quotes -
i have excel 2010 workbook. need save used range of each of worksheets tab-delimited text file no quotes, same filename workbook , extension given worksheet name.
note excel stupidly surrounds value quotes whenever sees comma, though delimiter tab; other that, normal "save as" / "text (tab delimited)" fine.
i prefer using vba code within excel.
if there python solution, i'd interested too. @ point pywin32 back upwards python 3 experimental, not sure can utilize it.
ok here complex routine wrote couple of months 1 of clients. code exports excel worksheet fixed width file without quotes. screenshots attached. sure code can made improve :)
tried , tested
option explicit '~~> alter relevant output filename , path const stroutputfile string = "c:\output.csv" sub sample() dim ws worksheet dim rng range dim myarray() long, maxlength long dim ff long, long, lastrow long, lastcol long dim stroutput string on error goto whoa application.screenupdating = false '~~> alter respective sheet set ws = sheets("sheet1") lastcol = ws.cells(1, columns.count).end(xltoleft).column '~~> loop through each column max size of field = 1 lastcol maxlength = getmaxlength(ws, i) redim preserve myarray(i) myarray(i) = maxlength next ff = freefile '~~> output file open stroutputfile output #ff '~~> write text file ws lastrow = .range("a" & rows.count).end(xlup).row each rng in .range("a1:a" & lastrow) rng = 1 ubound(myarray) '~~> insert delimiter here if text has spaces stroutput = stroutput & " " & left(.offset(0, i-1).text & _ string(myarray(i), " "), myarray(i)) next print #ff, mid(trim(stroutput), 1) stroutput = empty end next rng end letscontinue: on error resume next close #ff on error goto 0 application.screenupdating = true exit sub whoa: msgbox err.description resume letscontinue end sub '~~> function max size public function getmaxlength(ws worksheet, col long) long dim lastrow long, j long getmaxlength = 0 lastrow = ws.range("a" & ws.rows.count).end(-4162).row j = 1 lastrow if len(trim(ws.cells(j, col).value)) > getmaxlength _ getmaxlength = len(trim(ws.cells(j, col).value)) next j end function
excel-vba pywin32
Comments
Post a Comment