Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
85 views
in Technique[技术] by (71.8m points)

Python-docx in Google cloud functions

Has anyone tried using python-docx on google cloud functions? I am just getting started and can't get a simple code below work:

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

Edit 1:

Here's the log for above snippet:

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'

I thought this has something to do with the bucket storage thing and tried below but can't seem to get it to work. Appreciate help

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')

Edit 2:

Sorry, here is the log for snippet above:

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'
question from:https://stackoverflow.com/questions/66065713/python-docx-in-google-cloud-functions

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

On Cloud Functions the only path where you can write documents is on /tmp as mentioned here, so I rewrote the Cloud Function hello_world as:

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!'

And in the requirements.txt file added the next line:

python-docx

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...