Python-docx in Google cloud functions - python

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'

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

Related

pd.read_feather() cannot read feather file -- OSError: Verification of flatbuffer-encoded Footer failed

I have some py scripts to read data from db and write it to a feather file (with pandas to_feather() method). But somehow the written file cannot be read and it throws error as below:
Traceback (most recent call last):
File "/opt/***/***pipeline.py", line 303, in <module>
__main__()
File "/opt/***/***pipeline.py", line 124, in __main__
imt_preprocess.preprocess_data(config, thread_id=None, filtered_durations=durations, module=module, output_directory=output_directory, valid_from=valid_from, valid_until=valid_until)
File "/opt/***/***modules/imt_preprocess.py", line 27, in preprocess_data
df = commons.read_data(source_path)
File "/opt/***/***commons.py", line 62, in read_data
df = pd.read_feather(path)
File "/home/***/.local/lib/python3.7/site-packages/pandas/io/feather_format.py", line 131, in read_feather
handles.handle, columns=columns, use_threads=bool(use_threads)
File "/home/***/.local/lib/python3.7/site-packages/pyarrow/feather.py", line 220, in read_feather
return (read_table(source, columns=columns, memory_map=memory_map)
File "/home/***/.local/lib/python3.7/site-packages/pyarrow/feather.py", line 241, in read_table
reader = _feather.FeatherReader(source, use_memory_map=memory_map)
File "pyarrow/_feather.pyx", line 75, in pyarrow._feather.FeatherReader.__cinit__
File "pyarrow/error.pxi", line 143, in pyarrow.lib.pyarrow_internal_check_status
File "pyarrow/error.pxi", line 114, in pyarrow.lib.check_status
OSError: Verification of flatbuffer-encoded Footer failed.
Any idea what is the reason of this?

Download audio only youtube raises regexmatcherror

I am trying to execute the following code, that is supposed to download the audio file from youtube url
from pathlib import Path
from pytube import YouTube
def download_youtube_video(youtube_url, output_path):
audio_file = YouTube(youtube_url).streams.get_audio_only().download(output_path=output_path)
audio_file = Path(audio_file)
audio_file = audio_file.replace(audio_file.with_suffix(".mp3"))
return audio_file
youtube_url = 'https://youtu.be/_H5hsUwv8lE'
output_path = Path(__file__).parent
audio_file = download_youtube_video(youtube_url, output_path)
But I got the following traceback
Traceback (most recent call last):
File "C:\Users\Future\AppData\Local\Programs\Python\Python38\lib\site-packages\pytube\__main__.py", line 181, in fmt_streams
extract.apply_signature(stream_manifest, self.vid_info, self.js)
File "C:\Users\Future\AppData\Local\Programs\Python\Python38\lib\site-packages\pytube\extract.py", line 409, in apply_signature
cipher = Cipher(js=js)
File "C:\Users\Future\AppData\Local\Programs\Python\Python38\lib\site-packages\pytube\cipher.py", line 43, in __init__
self.throttling_plan = get_throttling_plan(js)
File "C:\Users\Future\AppData\Local\Programs\Python\Python38\lib\site-packages\pytube\cipher.py", line 405, in get_throttling_plan
raw_code = get_throttling_function_code(js)
File "C:\Users\Future\AppData\Local\Programs\Python\Python38\lib\site-packages\pytube\cipher.py", line 311, in get_throttling_function_code
name = re.escape(get_throttling_function_name(js))
File "C:\Users\Future\AppData\Local\Programs\Python\Python38\lib\site-packages\pytube\cipher.py", line 296, in get_throttling_function_name
raise RegexMatchError(
pytube.exceptions.RegexMatchError: get_throttling_function_name: could not find match for multiple
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "demo.py", line 18, in <module>
audio_file = download_youtube_video(youtube_url, output_path)
File "demo.py", line 6, in download_youtube_video
audio_file = YouTube(youtube_url).streams.get_audio_only().download(output_path=output_path)
File "C:\Users\Future\AppData\Local\Programs\Python\Python38\lib\site-packages\pytube\__main__.py", line 296, in streams
return StreamQuery(self.fmt_streams)
File "C:\Users\Future\AppData\Local\Programs\Python\Python38\lib\site-packages\pytube\__main__.py", line 188, in fmt_streams
extract.apply_signature(stream_manifest, self.vid_info, self.js)
File "C:\Users\Future\AppData\Local\Programs\Python\Python38\lib\site-packages\pytube\extract.py", line 409, in apply_signature
cipher = Cipher(js=js)
File "C:\Users\Future\AppData\Local\Programs\Python\Python38\lib\site-packages\pytube\cipher.py", line 43, in __init__
self.throttling_plan = get_throttling_plan(js)
File "C:\Users\Future\AppData\Local\Programs\Python\Python38\lib\site-packages\pytube\cipher.py", line 405, in get_throttling_plan
raw_code = get_throttling_function_code(js)
File "C:\Users\Future\AppData\Local\Programs\Python\Python38\lib\site-packages\pytube\cipher.py", line 311, in get_throttling_function_code
name = re.escape(get_throttling_function_name(js))
File "C:\Users\Future\AppData\Local\Programs\Python\Python38\lib\site-packages\pytube\cipher.py", line 296, in get_throttling_function_name
raise RegexMatchError(
pytube.exceptions.RegexMatchError: get_throttling_function_name: could not find match for multiple
Any idea how to fix such a problem?
My steps to solve the problem:
First, detect the package pytube path using the code
import pytube
import os
print(pytube.__file__)
print(os.path.dirname(pytube.__file__))
Navigate to the directory pytube and modify the cipher.py
The line 273 r'\([a-z]\s*=\s*([a-zA-Z0-9$]{3})(\[\d+\])?\([a-z]\)', changed to r'\([a-z]\s*=\s*([a-zA-Z0-9$]+)(\[\d+\])?\([a-z]\)',
The line 288 nfunc=function_match.group(1)), changed to nfunc=re.escape(function_match.group(1))),

google.auth.exceptions.DefaultCredentialsError

I am trying to connect to Google Speech-to-Text API and keep bumping into this error. I'm a noob python user, so thanks in advance for your help and patience!
PS C:\Users\LUVU\Desktop\TD\twistcube\td_speech_to_text> python 2_speech_fromaudio.py
Traceback (most recent call last):
File "C:\Users\LUVU\Desktop\TD\twistcube\td_speech_to_text\2_speech_fromaudio.py", line 11, in <module>
client = speech.SpeechClient()
File "C:\Users\LUVU\AppData\Local\Programs\Python\Python310\lib\site-packages\google\cloud\speech_v1\services\speech\client.py", line 408, in __init__
self._transport = Transport(
File "C:\Users\LUVU\AppData\Local\Programs\Python\Python310\lib\site-packages\google\cloud\speech_v1\services\speech\transports\grpc.py", line 151, in __init__
super().__init__(
File "C:\Users\LUVU\AppData\Local\Programs\Python\Python310\lib\site-packages\google\cloud\speech_v1\services\speech\transports\base.py", line 105, in __init__
credentials, _ = google.auth.default(
File "C:\Users\LUVU\AppData\Local\Programs\Python\Python310\lib\site-packages\google\auth\_default.py", line 544, in default
credentials, project_id = checker()
File "C:\Users\LUVU\AppData\Local\Programs\Python\Python310\lib\site-packages\google\auth\_default.py", line 537, in <lambda>
lambda: _get_explicit_environ_credentials(quota_project_id=quota_project_id),
File "C:\Users\LUVU\AppData\Local\Programs\Python\Python310\lib\site-packages\google\auth\_default.py", line 218, in _get_explicit_environ_credentials
credentials, project_id = load_credentials_from_file(
File "C:\Users\LUVU\AppData\Local\Programs\Python\Python310\lib\site-packages\google\auth\_default.py", line 117, in load_credentials_from_file
raise exceptions.DefaultCredentialsError(
google.auth.exceptions.DefaultCredentialsError: File [td-cc-342823-bda3eb899dc0] was not found.
PS C:\Users\LUVU\Desktop\TD\twistcube\td_speech_to_text>
Screenshot

Module 'cryptography.utils' has no attribute 'bit_length'

I am currently trying to figure out how to use netmiko to automate some of my routine work.Such as getting configuration backup, creating vlans etc.I've managed to use it on Aruba and Huawei Switches without problem.But on alcatel switch I'm facing this issue:
Unknown exception: module 'cryptography.utils' has no attribute 'bit_length'
Traceback (most recent call last):
File "C:\Users\melih.celik\AppData\Local\Programs\Python\Python39\lib\site-packages\paramiko\transport.py", line 2075, in run
self.kex_engine.parse_next(ptype, m)
File "C:\Users\melih.celik\AppData\Local\Programs\Python\Python39\lib\site-packages\paramiko\kex_gex.py", line 101, in parse_next
return self._parse_kexdh_gex_reply(m)
File "C:\Users\melih.celik\AppData\Local\Programs\Python\Python39\lib\site-packages\paramiko\kex_gex.py", line 281, in _parse_kexdh_gex_reply
self.transport._verify_key(host_key, sig)
File "C:\Users\melih.celik\AppData\Local\Programs\Python\Python39\lib\site-packages\paramiko\transport.py", line 1886, in _verify_key
if not key.verify_ssh_sig(self.H, Message(sig)):
File "C:\Users\melih.celik\AppData\Local\Programs\Python\Python39\lib\site-packages\paramiko\dsskey.py", line 148, in verify_ssh_sig
key = dsa.DSAPublicNumbers(
File "C:\Users\melih.celik\AppData\Local\Programs\Python\Python39\lib\site-packages\cryptography\hazmat\primitives\asymmetric\dsa.py", line 212, in public_key
return backend.load_dsa_public_numbers(self)
File "C:\Users\melih.celik\AppData\Local\Programs\Python\Python39\lib\site-packages\cryptography\hazmat\backends\openssl\backend.py", line 871, in load_dsa_public_numbers
dsa._check_dsa_parameters(numbers.parameter_numbers)
File "C:\Users\melih.celik\AppData\Local\Programs\Python\Python39\lib\site-packages\netmiko\tplink\tplink_jetstream.py", line 145, in _override_check_dsa_parameters
if crypto_utils.bit_length(parameters.q) not in [160, 256]:
AttributeError: module 'cryptography.utils' has no attribute 'bit_length'
Traceback (most recent call last):
File "C:\Users\melih.celik\Desktop\New_Backup\Yedek\Coding\Rand stuff\ssh_deneme(toplu).py", line 75, in
config_backup(cihaz_secim,ip_address,username,password)
File "C:\Users\melih.celik\Desktop\New_Backup\Yedek\Coding\Rand stuff\ssh_deneme(toplu).py", line 12, in config_backup
net_connect=ConnectHandler(**switch) #Baglanti kuruldu.
File "C:\Users\melih.celik\AppData\Local\Programs\Python\Python39\lib\site-packages\netmiko\ssh_dispatcher.py", line 312, in ConnectHandler
return ConnectionClass(*args, **kwargs)
File "C:\Users\melih.celik\AppData\Local\Programs\Python\Python39\lib\site-packages\netmiko\base_connection.py", line 346, in init
self._open()
File "C:\Users\melih.celik\AppData\Local\Programs\Python\Python39\lib\site-packages\netmiko\base_connection.py", line 351, in _open
self.establish_connection()
File "C:\Users\melih.celik\AppData\Local\Programs\Python\Python39\lib\site-packages\netmiko\base_connection.py", line 920, in establish_connection
self.remote_conn_pre.connect(**ssh_connect_params)
File "C:\Users\melih.celik\AppData\Local\Programs\Python\Python39\lib\site-packages\paramiko\client.py", line 406, in connect
t.start_client(timeout=timeout)
File "C:\Users\melih.celik\AppData\Local\Programs\Python\Python39\lib\site-packages\paramiko\transport.py", line 660, in start_client
raise e
File "C:\Users\melih.celik\AppData\Local\Programs\Python\Python39\lib\site-packages\paramiko\transport.py", line 2075, in run
self.kex_engine.parse_next(ptype, m)
File "C:\Users\melih.celik\AppData\Local\Programs\Python\Python39\lib\site-packages\paramiko\kex_gex.py", line 101, in parse_next
return self._parse_kexdh_gex_reply(m)
File "C:\Users\melih.celik\AppData\Local\Programs\Python\Python39\lib\site-packages\paramiko\kex_gex.py", line 281, in _parse_kexdh_gex_reply
self.transport._verify_key(host_key, sig)
File "C:\Users\melih.celik\AppData\Local\Programs\Python\Python39\lib\site-packages\paramiko\transport.py", line 1886, in _verify_key
if not key.verify_ssh_sig(self.H, Message(sig)):
File "C:\Users\melih.celik\AppData\Local\Programs\Python\Python39\lib\site-packages\paramiko\dsskey.py", line 148, in verify_ssh_sig
key = dsa.DSAPublicNumbers(
File "C:\Users\melih.celik\AppData\Local\Programs\Python\Python39\lib\site-packages\cryptography\hazmat\primitives\asymmetric\dsa.py", line 212, in public_key
return backend.load_dsa_public_numbers(self)
File "C:\Users\melih.celik\AppData\Local\Programs\Python\Python39\lib\site-packages\cryptography\hazmat\backends\openssl\backend.py", line 871, in load_dsa_public_numbers
dsa._check_dsa_parameters(numbers.parameter_numbers)
File "C:\Users\melih.celik\AppData\Local\Programs\Python\Python39\lib\site-packages\netmiko\tplink\tplink_jetstream.py", line 145, in _override_check_dsa_parameters
if crypto_utils.bit_length(parameters.q) not in [160, 256]:
AttributeError: module 'cryptography.utils' has no attribute 'bit_length'
Thanks for your help in advance.
It looks like tplink_jetstream.py assumes that a (now removed) helper function is available to it. The simplest fix here would be to go into that file and modify the line containing crypto_utils.bit_length(parameters.q) to read parameters.q.bit_length() instead.

pyglet errors for python3

The pyglet module on my computer isn't working. I've tried a lot of suggestions from others and none of them have worked.
First I tried pip3 install pyglet. But when I tried to run code I got this error:
Traceback (most recent call last):
File "/Users/chervjay/Downloads/gameloop.py", line 5, in <module>
from pyglet.gl import *
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/pyglet/gl/__init__.py", line 236, in <module>
import pyglet.window
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/pyglet/window/__init__.py", line 1816, in <module>
gl._create_shadow_window()
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/pyglet/gl/__init__.py", line 205, in _create_shadow_window
_shadow_window = Window(width=1, height=1, visible=False)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/pyglet/window/__init__.py", line 493, in __init__
display = get_platform().get_default_display()
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/pyglet/window/__init__.py", line 1765, in get_default_display
return pyglet.canvas.get_display()
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/pyglet/canvas/__init__.py", line 82, in get_display
return Display()
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/pyglet/canvas/carbon.py", line 22, in __init__
import MacOS
ImportError: No module named 'MacOS'
Then I commented out the lines that use MacOS in carbon.py, which are only 2, and ran it again, and this time it said
Traceback (most recent call last):
File "/Users/chervjay/Downloads/gameloop.py", line 132, in <module>
app = App()
File "/Users/chervjay/Downloads/gameloop.py", line 114, in __init__
self.hud = Hud(self.win)
File "/Users/chervjay/Downloads/gameloop.py", line 97, in __init__
color=(1, 1, 1, 0.5),
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/pyglet/font/__init__.py", line 348, in __init__
group=self._group)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/pyglet/text/layout.py", line 791, in __init__
self.document = document
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/pyglet/text/layout.py", line 874, in _set_document
self._init_document()
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/pyglet/text/layout.py", line 977, in _init_document
self._update()
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/pyglet/text/layout.py", line 911, in _update
lines = self._get_lines()
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/pyglet/text/layout.py", line 887, in _get_lines
glyphs = self._get_glyphs()
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/pyglet/text/layout.py", line 1016, in _get_glyphs
glyphs.extend(font.get_glyphs(text[start:end]))
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/pyglet/font/base.py", line 378, in get_glyphs
self.glyphs[c] = glyph_renderer.render(c)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/pyglet/font/carbon.py", line 240, in render
text_ucs2 = str_ucs2(text)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/pyglet/font/carbon.py", line 194, in str_ucs2
return create_string_buffer(text + '\0')
TypeError: can't concat bytes to str
I have tried
pip3.5 install --upgrade http://pyglet.googlecode.com/archive/tip.zip and got the same errors. I cannot find any other answers.
Please help if you know how to. Thank you.
I got the code I'm trying to run from http://tartley.com/?p=250.

Categories

Resources