Python-docx 在 Google 云函数中

Python-docx in Google cloud functions

有人试过在 google 云函数上使用 python-docx 吗? 我才刚刚开始,无法使用下面的简单代码:

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

编辑 1:

这是上面片段的日志:

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:

抱歉,这是上面片段的日志:

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'

在 Cloud Functions 上,您可以编写文档的唯一路径是 /tmp,如前所述 ,因此我将 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