Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Save no longer allows non-Excel formats in version 0.23.1 #1569

Closed
FERNLEAduglee opened this issue Apr 26, 2021 · 8 comments
Closed

Save no longer allows non-Excel formats in version 0.23.1 #1569

FERNLEAduglee opened this issue Apr 26, 2021 · 8 comments
Labels
Milestone

Comments

@FERNLEAduglee
Copy link

FERNLEAduglee commented Apr 26, 2021

Windows 10 Enterprise 64-bit

Python 3.7.4
xlwings 0.23.1
Excel 2019

Prior to version 0.23.1, I could save a book in alternate formats, such as '.txt' just by specifiying the appropriate filename/extention. That now gives a "KeyError: '.txt'" This was working perfectly until I updated to 0.23.1.

Sample Code:
activeBook = books('DL2SALES.XLS')
activeBook.save(path + fileName+'.txt')

Traceback:
File "c:/Users/dougl/Documents/Python/Fernlea/Costing Data to Excel/Main21.py", line 513, in
if Flags['Items']: performProcess(ProcessList, 'Item Master')
File "c:/Users/dougl/Documents/Python/Fernlea/Costing Data to Excel/Main21.py", line 162, in performProcess
saveExcelFile(currentAction[1], basePath, basePath1)
File "c:/Users/dougl/Documents/Python/Fernlea/Costing Data to Excel/Main21.py", line 420, in saveExcelFile
activeBook.save(path + fileName+'.txt')
File "C:\Program Files\Python37\lib\site-packages\xlwings\main.py", line 740, in save
self.impl.save(path)
File "C:\Program Files\Python37\lib\site-packages\xlwings_xlwindows.py", line 533, in save
file_format = ext_to_file_format[target_ext]
KeyError: '.txt'

EDIT: downgrading to 0.22.3 has eliminated the issue.

@fzumstein
Copy link
Member

Was this previously giving you a tab-delimited Text file?

@FERNLEAduglee
Copy link
Author

Yes. I have been using it for about 20 months, and it was working perfectly until I updated to 0.23.1

@fzumstein
Copy link
Member

yes, 0.23.1 added support for the Excel file formats, and ignored the non-Excel file formats for now. It should be easy to add.

@fzumstein fzumstein added this to the 0.23.2 milestone Apr 26, 2021
@fzumstein fzumstein added the bug label Apr 26, 2021
@FERNLEAduglee
Copy link
Author

Thanks

@fzumstein
Copy link
Member

Actually, can you post a fully self-contained example of what you're doing/achieving? I tried the following with 0.22.3, which just gives me an Excel file with a txt ending (but not a text file).

import xlwings as xw

wb = xw.Book()
wb.save('YYY.txt')

One way to export to CSV would be:

import xlwings as xw
import csv

sheet = xw.books.active.sheets.active

with open('mycsv.csv', 'w') as csvfile:
    writer = csv.writer(csvfile)
    writer.writerows(sheet.used_range.value)

@FERNLEAduglee
Copy link
Author

Based on what your comments, below I did a little further research.

This portion of my code causes a file to be downloaded from a central system. The file automatically appears loaded in Excel on my system, and then I save it using activeBook.save. However, I was not aware that the file structure that I receive is actually tab-delimited text (.txt). I found that when I go to File/Save As in Excel, the file format is noted as Text (Tab Delimited) (*.txt). So when I save it as a .txt, it is already in that format and therefore is saved in that format, regardless of the filename I choose.

It appears that 0.22.3 actually does not do any conversion at all. A new book is saved as .xls, and an existing book is saved as whatever format it was when it was loaded into Excel.

When 0.23.1 was implemented, it altered the save routine such that using the .txt extension is no longer recognized and therefore the Python script fails.

Rewriting my code to use csv would not be my most preferred option. I am happy at this time to stay on 0.22.3 until such time as the kinks in the new save routine are ironed out, hoping of course that saving to .txt will be implemented along with other formats.

For reference, here is the code snippet and a sample file:

def saveExcelFile(fileName, path):
    if logFlag:
        log("Saving Excel file:" + fileName)
    activeBook = books('DL2SALES.XLS')
    activeBook.save(path + '\\' + fileName+'.txt')
    sleep(0.5)

DL2SALES.XLS

@fzumstein
Copy link
Member

Ok that explains the behavior then. Out of curiosity, why would you even need Excel to change a pure text file?

@FERNLEAduglee
Copy link
Author

FERNLEAduglee commented Apr 29, 2021 via email

fzumstein added a commit that referenced this issue May 6, 2021
fzumstein added a commit that referenced this issue May 6, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants