Excelドキュメントのデータを含むBytesIOオブジェクトがあります。使用したいライブラリはBytesIOをサポートしておらず、代わりにFileオブジェクトが必要です。BytesIOオブジェクトを取得してFileオブジェクトに変換するにはどうすればよいですか?
Excelファイルでの作業に使用していたライブラリを提供すると便利ですが、これは私が行っているいくつかの仮定に基づいたソリューションのバックショットです。
。
import io
b = io.BytesIO(b"Hello World") ## Some random BytesIO Object
print(type(b)) ## For sanity's sake
with open("test.xlsx") as f: ## Excel File
print(type(f)) ## Open file is TextIOWrapper
bw=io.TextIOWrapper(b) ## Conversion to TextIOWrapper
print(type(bw)) ## Just to confirm
。
import io
import os
with open("test.xlsx",'rb') as f:
g=io.BytesIO(f.read()) ## Getting an Excel File represented as a BytesIO Object
temporarylocation="testout.xlsx"
with open(temporarylocation,'wb') as out: ## Open temporary file as bytes
out.write(g.read()) ## Read bytes into file
## Do stuff with module/file
os.remove(temporarylocation) ## Delete file when done
これらの点の1つがあなたの問題を解決することを願っています。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加