【问题标题】:Python-docx in Google cloud functions谷歌云函数中的 Python-docx
【发布时间】:2021-05-09 22:32:24
【问题描述】:

有没有人尝试在谷歌云功能上使用 python-docx? 我才刚刚开始,无法获得以下工作的简单代码:

from docx import Document
document = Document('blank_doc.docx')
document.save('test.docx');

编辑 1:

这是上面 sn-p 的日志:

2021-02-05T15:39:56.658Ztest1w0yivt7dw3gw Traceback (most recent call last): 
File "/env/local/lib/python3.7/site-packages/google/cloud/functions/worker_v2.py", 
line 402, in run_http_function result = _function_handler.invoke_user_function(flask.request) 
File "/env/local/lib/python3.7/site-packages/google/cloud/functions/worker_v2.py", 
line 268, in invoke_user_function return call_user_function(request_or_event) 
File "/env/local/lib/python3.7/site-packages/google/cloud/functions/worker_v2.py", 
line 261, in call_user_function return self._user_function(request_or_event) 
File "/user_code/main.py", 
line 6, in test1 document.save('document.docx') 
File "/env/local/lib/python3.7/site-packages/docx/document.py", 
line 135, in save self._part.save(path_or_stream) 
File "/env/local/lib/python3.7/site-packages/docx/parts/document.py", 
line 111, in save self.package.save(path_or_stream) 
File "/env/local/lib/python3.7/site-packages/docx/opc/package.py", 
line 172, in save PackageWriter.write(pkg_file, self.rels, self.parts) 
File "/env/local/lib/python3.7/site-packages/docx/opc/pkgwriter.py", 
line 32, in write phys_writer = PhysPkgWriter(pkg_file) 
File "/env/local/lib/python3.7/site-packages/docx/opc/phys_pkg.py", 
line 141, in __init__ self._zipf = ZipFile(pkg_file, 'w', compression=ZIP_DEFLATED) 
File "/opt/python3.7/lib/python3.7/zipfile.py", 
line 1240, in __init__ self.fp = io.open(file, filemode) OSError: [Errno 30] Read-only file system: 'document.docx'

Traceback (most recent call last): File "/env/local/lib/python3.7/site-packages/google/cloud/functions/worker_v2.py", 
line 402, in run_http_function result = _function_handler.invoke_user_function(flask.request) 
File "/env/local/lib/python3.7/site-packages/google/cloud/functions/worker_v2.py", 
line 268, in invoke_user_function return call_user_function(request_or_event) 
File "/env/local/lib/python3.7/site-packages/google/cloud/functions/worker_v2.py", 
line 261, in call_user_function return self._user_function(request_or_event) 
File "/user_code/main.py", line 6, in test1 document.save('document.docx')
File "/env/local/lib/python3.7/site-packages/docx/document.py", 
line 135, in save self._part.save(path_or_stream) 
File "/env/local/lib/python3.7/site-packages/docx/parts/document.py", 
line 111, in save self.package.save(path_or_stream) 
File "/env/local/lib/python3.7/site-packages/docx/opc/package.py", 
line 172, in save PackageWriter.write(pkg_file, self.rels, self.parts) 
File "/env/local/lib/python3.7/site-packages/docx/opc/pkgwriter.py", 
line 32, in write phys_writer = PhysPkgWriter(pkg_file) 
File "/env/local/lib/python3.7/site-packages/docx/opc/phys_pkg.py", 
line 141, in __init__ self._zipf = ZipFile(pkg_file, 'w', compression=ZIP_DEFLATED) 
File "/opt/python3.7/lib/python3.7/zipfile.py", 
line 1240, in __init__ self.fp = io.open(file, filemode) OSError: [Errno 30] Read-only file system: 'document.docx'

我认为这与存储桶有关,并在下面尝试过,但似乎无法正常工作。感谢帮助

from google.cloud import storage
from docx import Document   
client = storage.Client()
import io

def test(request):
    file_stream = io.BytesIO()

    BUCKET = 'out_bucket'
    document = Document('blank_doc.docx')
    bucket = client.get_bucket(BUCKET)
    document.save(file_stream)
    file_stream = file_stream.encode('utf-8')
    newblob = bucket.blob(file_stream)
    newblob.upload_from_file('document.docx')

编辑 2:

对不起,这里是上面sn-p的日志:

2021-02-05T15:30:53.665Ztest18s7xo9gksz2 Traceback (most recent call last): 
File "/env/local/lib/python3.7/site-packages/google/cloud/functions/worker_v2.py", 
line 402, in run_http_function result = _function_handler.invoke_user_function(flask.request) 
File "/env/local/lib/python3.7/site-packages/google/cloud/functions/worker_v2.py", 
line 268, in invoke_user_function return call_user_function(request_or_event) 
File "/env/local/lib/python3.7/site-packages/google/cloud/functions/worker_v2.py", 
line 261, in call_user_function return self._user_function(request_or_event) 
File "/user_code/main.py", 
line 13, in test file_stream = file_stream.encode('utf-8') AttributeError: 
'_io.BytesIO' object has no attribute 'encode'


Traceback (most recent call last): 
File "/env/local/lib/python3.7/site-packages/google/cloud/functions/worker_v2.py", 
line 402, in run_http_function result = _function_handler.invoke_user_function(flask.request) 
File "/env/local/lib/python3.7/site-packages/google/cloud/functions/worker_v2.py", 
line 268, in invoke_user_function return call_user_function(request_or_event) 
File "/env/local/lib/python3.7/site-packages/google/cloud/functions/worker_v2.py", 
line 261, in call_user_function return self._user_function(request_or_event) 
File "/user_code/main.py", 
line 13, in test file_stream = file_stream.encode('utf-8') AttributeError: 
'_io.BytesIO' object has no attribute 'encode'

【问题讨论】:

  • 你应该发布你得到的错误。
  • 既然知道自己是菜鸟,我就觉得我的代码明显不正确,一开始就没有添加任何日志。现已添加,感谢您的宝贵时间。
  • 回溯的关键部分似乎是“只读文件系统”。我希望如果您注释掉 .save() 行,它不会引发错误。您需要在某个地方保存结果,或者您可以将其保存到 BytesIO“内存中”文件并通过 HTTP 响应正文或其他内容返回该文件的内容。下次将回溯格式化为每行一个部分。否则,您会要求读者做比他们可能做的更多的工作来帮助您。

标签: python google-cloud-functions python-docx


【解决方案1】:

在 Cloud Functions 上,您可以编写文档的唯一路径是 /tmp,如 here 所述,因此我将 Cloud Functions hello_world 重写为:

from docx import Document

def hello_world(request):

    document = Document()
    document.save('/tmp/test.docx');

    request_json = request.get_json()
    if request.args and 'message' in request.args:
        return request.args.get('message')
    elif request_json and 'message' in request_json:
        return request_json['message']
    else:
        return f'Hello World!'

并在requirements.txt 文件中添加了下一行:

python-docx

【讨论】:

  • 非常感谢您的回答。它有效,我现在想弄清楚如何将这些数据放入输出存储桶中
  • 也许这个information 可以提供帮助,但是如果您在尝试这样做时遇到问题,请提出一个新问题,我可以为您提供帮助
猜你喜欢
  • 2019-02-15
  • 2021-03-04
  • 2020-11-21
  • 2021-01-12
  • 2020-06-17
  • 1970-01-01
  • 2018-12-07
  • 2018-10-02
  • 2021-01-22
相关资源
最近更新 更多