-
Notifications
You must be signed in to change notification settings - Fork 90
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
Capability to return a BytesIO/filelike even if it isn't encrypted? #85
Comments
Thanks for your suggestion!
I understand. The problem is that always creating a BytesIO object can consume unnecessary memory, especially if the document is large in file size. |
Brilliant, I just threw that together to get what I needed, I'm sure you'll make it better 😄
~ snip ~ I wrote a load of nonsense, I'm sure you know what you're doing! Plus, this way can support alternatives to just BytesIO which is nice, I guess you could |
There will probably be a lot of people like me who forgot to with open(file_path, 'rb') as f:
file = msoffcrypto.OfficeFile(f)
if file.is_encrypted():
file.load_key(password=password) # Use password
file.decrypt(decrypted_io)
else:
f.seek(0) # don't forget this
decrypted_io.write(f.read())
return decrypted_io |
Hello,
I understand that this is potentially out of scope for the project, but considering the existence of
OfficeFile.is_encrypted()
I feel this would tie its usage up nicely.I'll explain a use case via example:
I am using this to load up a set of usually-encrypted Excel files into
pandas
, this is great, except a handful of these Excel files have randomly have not been password protected. I don't actually care whether or not they have a password, I just want to put them all into dataframes.Right now, the argument I pass to
pandas.read_excel()
is either a non-protected Excel file'sPath
, or aBytesIO
objected retrieved using this library.This is fine but it has resulted in this messy function:
And then I just have to hope everything downstream is cool with taking either a
BytesIO
or astr
/Path
, which is okay for pandas but I imagine is less okay for other libraries/use cases.I'm not sure how it would be best to insert the functionality, but something like
OfficeFile.to_bytes()
(I'm sure there are better ideas for function names available) would be great, then we can have consistent return types.I also find it really odd that
.decrypt()
takes the object you want to inject the file into as an argument, rather than returning a BytesIO object? It makes following the code flow feel awkward to me, but that's an issue for another day!The text was updated successfully, but these errors were encountered: