【问题标题】:Django open excel.xlsx with openpyxl from Google Cloud StorageDjango 使用 Google Cloud Storage 中的 openpyxl 打开 excel.xlsx
【发布时间】:2021-12-09 12:59:47
【问题描述】:

我需要从 Google Cloud Storage 上的存储桶中打开一个 .xlsx 文件,问题是我在 /api/ficha-excel 处得到 :FileNotFoundError

[Errno 2] 没有这样的文件或目录:'ficha.xlsx'

这些是我存储桶中的设置。

UPLOAD_ROOT = 'reportes/'
MEDIA_ROOT = 'reportes'

这些是路由bucket/reportes/ficha.xlsx

这是我的get函数的代码:

    directorio = FileSystemStorage("/reportes").base_location
    os.makedirs(directorio, exist_ok=True)
    # read
    print("Directorios: ", directorio)
    plantilla_excel = openpyxl.load_workbook(f"{directorio}/ficha.xlsx")
    print(plantilla_excel.sheetnames)
    currentSheet = plantilla_excel['Hoja1']
    print(currentSheet['A5'].value)

路径有什么问题?我想不通。

【问题讨论】:

  • 根据documentation,FileSystemStorage 类的 location 参数需要有一个指向保存文件的目录的绝对路径。该位置默认为您的 MEDIA_ROOT 设置的值。我建议您尝试使用绝对路径设置 MEDIA_ROOT 和位置,例如。 'gs://mybucket/folder/file.xlsx'。还为读取/查看存储对象提供适当的权限。
  • @PriyashreeBhadra 使用 default_storage 怎么样?我还需要将 media_root 定义为绝对路径吗?
  • 如何编辑到绝对路径?这是我的设置: DEFAULT_FILE_STORAGE = 'storages.backends.gcloud.GoogleCloudStorage' GS_PROJECT_ID = 'green-carrier-xxxxxx' GS_BUCKET_NAME = 'inversiones-admision' UPLOAD_ROOT = 'reportes/' MEDIA_ROOT = 'reportes' MEDIA_URL = 'storage.googleapis.com{ }/'.format(GS_BUCKET_NAME) ,据我所知,我必须把它放在视图上? plantilla_excel = openpyxl.load_workbook("//inversiones-admision/ficha.xlsx")
  • 我认为this 应该回答你的问题。尝试让我知道它是否有效。
  • 我已经这样做了,我可以使用 django-storages 和 default_storage 完善我的存储桶上打开的文件,问题是我需要使用 openpyxl 读取文件并且我无法提取或获取路径打开的文件。

标签: django excel django-rest-framework google-cloud-storage openpyxl


【解决方案1】:

以下解决方案不使用 Django FileStorage/Storage 类。它使用 openpyxl 从 Google Storage 上的 Cloud Storage 存储桶打开一个 .xlsx 文件。

总结:

我在 GCS 上上传 Excel 文件,通过 BytesIO 使用 openpyxl 读取 Blob 数据,并使用 .save() 方法将数据保存在工作簿中。

要遵循的步骤:

创建一个 Google Cloud Storage 存储分区。为其选择一个全球唯一的名称。保持默认,最后进入Create。

从本地系统中选择一个 Excel 文件,然后使用“上传文件”选项将其上传到存储桶中。 将 excel 文件放入存储桶后,请按照以下步骤操作:

  • 转到 Google Cloud Platform 并创建一个服务帐户 (API)。点击 Navigation Menu> APIs & Services> Credentials 去屏幕。 然后点击管理服务帐户。

  • 在下一个屏幕上,点击创建服务帐户。

  • 输入每个项目的服务帐户的详细信息。

  • 在下一部分中,您将为 Cloud Storage 创建一个角色。选择 存储管理员(完全权限)。

  • 点击你创建的服务账号,点击Keys中的Add Key 字段,然后选择创建新密钥。

  • 选择 JSON 作为密钥类型并“创建”它。由于 JSON 文件是 下载到本地存储,使用下一项中的JSON文件 并通过 Python 操作 Cloud Storage。

  • 我们将在 Cloud 中安装此项目所需的库 Shell 首先,用 pip 安装 Google Cloud Storage 库 安装以访问 Cloud Storage:

    pip install google-cloud-storage

    使用以下命令安装 openpyxl:

    pip install openpyxl

  • 在云编辑器中使用您选择的名称创建一个文件夹 (excel)。 在其中创建文件:

    main.py JSON 密钥文件(在本地存储中下载的那个,复制那个 文件到这个文件夹)

    擅长 主文件 ●●●●●●●●●●.json

在 main.py 文件中编写以下代码行:

from google.cloud import storage
import openpyxl
import io

#Create a client instance for google cloud storage
client = storage.Client.from_service_account_json('●●●●●●●●●●.json') //The path to your JSON key file which is now 
#Get an instance of a bucket
bucket = client.bucket(‘bucket_name’) //only the bucketname will do, full path not necessary.

##Get a blob instance of a file
blob = bucket.blob(‘test.xlsx') // test.xlsx is the excel file I uploaded in the bucket already.
buffer = io.BytesIO()
blob.download_to_file(buffer)
wb = openpyxl.load_workbook(buffer)
wb.save('./retest.xlsx')

您将看到在 Cloud Editor 的同一文件夹中创建了一个文件“retest.xlsx”。

【讨论】:

  • 很抱歉没有回复这个,我真的很感谢你的时间和精力以及你的帮助,我接近这个项目的最后期限,所以我没有测试和回答,但很快我会.非常感谢!
  • 不用担心。只要有时间,请尝试并测试该解决方案,如果您认为我的回答有用,请单击其左侧的投票按钮 (▲)。如果它回答了您的问题,请单击复选标记 (✓) 接受它。这样其他人就知道你得到了(足够的)帮助。
猜你喜欢
  • 1970-01-01
  • 2015-08-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多