使用 Python 中的 Pandas 将多 sheet excel 文件保存到 Azure Blob 存储
Saving multi-sheet excel file to Azure Blob Storage using Pandas in Python
我正在尝试在 excel 文件中创建两个工作表,然后使用 Python 中的 Pandas 将其上传到 Azure Blob 存储。但是,我收到一个错误 - “无法 .getvalue() of 'OpenpyxlWriter' 对象 new_writer”。
blob_client = blob_service_client.get_blob_client(container=new_container_name, blob=file_name)
writer = io.BytesIO()
summary = pd.DataFrame({"one_val": [1, 2]})
summary.to_excel(writer, sheet_name="Sheet A", index=False)
blob_client.upload_blob(writer.getvalue(), overwrite=True)
writer = io.BytesIO()
summary2 = pd.DataFrame({"two_val": [3, 4]})
with pd.ExcelWriter(writer, engine="openpyxl", mode="a") as new_writer:
summary2.to_excel(new_writer, sheet_name="Sheet B", index=False, encoding="utf-8")
blob_client.upload_blob(new_writer.getvalue(), blob_type="AppendBlob", overwrite=False)
感谢任何有关此问题解决方案的帮助。谢谢
阅读本文了解如何将 xlsx 保存为字符串
https://xlsxwriter.readthedocs.io/working_with_pandas.html#saving-the-dataframe-output-to-a-string
这用于处理多张纸
https://xlsxwriter.readthedocs.io/example_pandas_multiple.html
最后一个例子是
import pandas as pd
import io
# Create some Pandas dataframes from some data.
df1 = pd.DataFrame({'Data': [11, 12, 13, 14]})
df2 = pd.DataFrame({'Data': [21, 22, 23, 24]})
output = io.BytesIO()
writer = pd.ExcelWriter(output, engine='xlsxwriter')
# Write the data frame to the BytesIO object.
df1.to_excel(writer, sheet_name='Sheet1')
df2.to_excel(writer, sheet_name='Sheet2')
writer.save()
xlsx_data = output.getvalue()
我正在尝试在 excel 文件中创建两个工作表,然后使用 Python 中的 Pandas 将其上传到 Azure Blob 存储。但是,我收到一个错误 - “无法 .getvalue() of 'OpenpyxlWriter' 对象 new_writer”。
blob_client = blob_service_client.get_blob_client(container=new_container_name, blob=file_name)
writer = io.BytesIO()
summary = pd.DataFrame({"one_val": [1, 2]})
summary.to_excel(writer, sheet_name="Sheet A", index=False)
blob_client.upload_blob(writer.getvalue(), overwrite=True)
writer = io.BytesIO()
summary2 = pd.DataFrame({"two_val": [3, 4]})
with pd.ExcelWriter(writer, engine="openpyxl", mode="a") as new_writer:
summary2.to_excel(new_writer, sheet_name="Sheet B", index=False, encoding="utf-8")
blob_client.upload_blob(new_writer.getvalue(), blob_type="AppendBlob", overwrite=False)
感谢任何有关此问题解决方案的帮助。谢谢
阅读本文了解如何将 xlsx 保存为字符串 https://xlsxwriter.readthedocs.io/working_with_pandas.html#saving-the-dataframe-output-to-a-string
这用于处理多张纸 https://xlsxwriter.readthedocs.io/example_pandas_multiple.html
最后一个例子是
import pandas as pd
import io
# Create some Pandas dataframes from some data.
df1 = pd.DataFrame({'Data': [11, 12, 13, 14]})
df2 = pd.DataFrame({'Data': [21, 22, 23, 24]})
output = io.BytesIO()
writer = pd.ExcelWriter(output, engine='xlsxwriter')
# Write the data frame to the BytesIO object.
df1.to_excel(writer, sheet_name='Sheet1')
df2.to_excel(writer, sheet_name='Sheet2')
writer.save()
xlsx_data = output.getvalue()