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