python-docx import is not able to be imported even though the library is installed - python-3.x

System:
MacBook Pro (13-inch, 2020, Two Thunderbolt 3 ports)
Processor 1.4 GHz Quad-Core Intel Core i5
Memory 16GB
OS macOs Monterey version 12.6.1
I'm still fairly new to Python and just learned about the docx library.
I get the following error in Visual Studio Code about the docx library.
Import "docx" could not be resolved.
enter image description here
When I check to see the version installed I get the following:
pip3 show python-docx
Name: python-docx
Version: 0.8.11
I am able to create Word documents with one Python script even with the import issue. However, I've tried to create one using a table and this is what is causing me issues. I'm not sure if the import issue is the root cause.
When I run my script I get the following:
python3 test.py
/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/docx/styles/styles.py:139: UserWarning: style lookup by style_id is deprecated. Use style name as key instead.
return self._get_style_id_from_style(self[style_name], style_type)`
The code causing the error:
Add review question table
table = document.add_table(rows=1, cols=3)
table.style = 'TableGrid'
In researching I found I may need to import the following:
from docx.oxml.table import CT_TableStyle
And add the following to that section:
# Add review question table
table = document.add_table(rows=1, cols=3)
style = CT_TableStyle()
style.name = 'TableGrid'
table.style = style
I now get the following warning:
Import "docx.oxml.table" could not be resolved
And the following error when running the script:
line 2, in
from docx.oxml.table import CT_TableStyle
ImportError: cannot import name 'CT_TableStyle' from 'docx.oxml.table' (/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/docx/oxml/table.py)
I also created a virtual environment and still have the same issues. If you need additional details, just let me know what to provide.
Kind regards,
Marcus

Related

How to silence warning from import IPython.nbformat: "UserWarning: nbformat.current is deprecated."

I'm working with the example code for importing an IPython (Jupyter) notebook,
Importing Notebooks. The example code still runs fine, however it generates a warning that I would like to understand and fix:
site-packages/nbformat/current.py:15: UserWarning: nbformat.current is deprecated.
- use nbformat for read/write/validate public API
- use nbformat.vX directly to composing notebooks of a particular version
warnings.warn("""nbformat.current is deprecated.
This warning has been discussed since 2015, at least, and yet I cannot find any constructive advice about what to do about it. Is this a warning that can be addressed by fixing code, or is it a function that will disappear from IPython without a replacement?
If you follow the link to the IPython blog, they claim that there is a newer version, but their link points to a non-existent page.
This code example is widely discussed in other threads in Stack Overflow, for example
python access functions in ipython notebook
Keep in mind the example you've linked up is for a much older version of Jupyter - 4.x. The page with these examples has been relocated at some point, and for the 5.7.6 version of Jupyter (the most recent as of writing this) it's located here.
First, replace the from IPython.nbformat import current import with ​from nbformat import read
Then, replace this part of the 4.x snippet:
with io.open(path, 'r', encoding='utf-8') as f:
nb = current.read(f, 'json')
with the newer version:
with io.open(path, 'r', encoding='utf-8') as f:
nb = read(f, 4)

What are Python3 libraries which replace "from scikits.audiolab import Format, Sndfile"

Hope you'll are doing good. I am new to python. I am trying to use audio.scikits library in python3 verion. I have a working code version in 2.7(with audio.scikits) . While I am running with python3 version I am getting the Import Error: No Module Named 'Version' error. I get to know that python3 is not anymore supporting audio.scikits(If I am not wrong). Can anyone suggest me replacing library for audio.scikits where I can use all the functionalities like audio.scikits do OR any other solution which might helps me. Thanks in advance.
2.7 Version Code :
from scikits.audiolab import Format, Sndfile
from scipy.signal import firwin, lfilter
array = np.array(all)
fmt = Format('flac', 'pcm16')
nchannels = 1
cd, FileNameTmp = mkstemp('TmpSpeechFile.wav')
# making the file .flac
afile = Sndfile(FileNameTmp, 'w', fmt, nchannels, RawRate)
#writing in the file
afile.write_frames(array)
SendSpeech(FileNameTmp)
To check entire code please visit :Google Asterisk Reference Code(modifying based on this code)
I want to modify this code with python3 supported libraries. Here I am doing this for Asterisk-Microsoft-Speech To Text SDK.
Firstly the link code you paste is Asterisk-Google-Speech-Recognition, it's not the Microsoft-Speech-To-Text, if you want get a sample about Microsoft-Speech-To-Text you could refer to the official doc:Recognize speech from an audio file.
And about your problem you said, yes it's not completely compatible, in the github issue there is a solution for it, you could refer to this comment.

how to use "Type Hints - PEP 484" when creating new objects in python 3.7 to be able code completion in pycharm 2019.2.4

system: fedora 31
Pycharm doesn't get me auto completion when type my_var_hash. , there is no problem with venv.
How use Type-Hints with modules like hashlib and that way can we use auto complete options (CTRL+space).
Example of code bellow but auto completion doesn't work.(functions: my_var_hash.update(b'lmao') , my_var_hash.hexdigest() ).
import hashlib
my_var_hash = hashlib.sha256()
print(my_var_hash)
my_var_hash.update(b'lmao')
print(my_var_hash.hexdigest())
print(type(my_var_hash.hexdigest()))
the problem is that hashlib.sha256() does not have type defined or some docstring (when using standard cptyhon)
PyCharm therefore assigns it the "Any" type that does not have any extra info
This is a library issue, not a problem with your setup

Import of Ecoinvent 2.2 and Ecoinvent 3.x fail with Brightway

The import of Ecoinvent 2.2 and 3.x does not happen. I do not understand what is the issue here.
I have downloaded both 2 and 3 versions from Ecoinvent.org but both the Ecospold1 and 2 importers show me the same (or the lack of) results.
`from brightway2 import *
ei33cutoff = SingleOutputEcospold2Importer(
r"C:\Users\HS254581\Documents\test\ecoinvent33cutoff\datasets",
"ecoinvent 3.3 cutoff"
)
ei33cutoff.apply_strategies()
ei33cutoff.statistics()`
All I get is
"Extracting XML data from 13831 datasets"
WITHOUT any error message. I have kept it running for hours sometimes, ultimately I have to quit.
I can however import Forwast database, do some LCA calculations here and some CSV files from Simapro.
I read most of the questions answered here and on github, but cannot find a solution to this problem.
Send help!
P.S. I am using Python 3.7 with Spyder. Here I need to activate bw2 environment in Anaconda prompt and set Python interpreter path to default. Have installed kernels too. This is the only way it seems to work. Also updated conda and brightway packages today just in case.

IAC-protocol interface error on python 3

I would like to work with excell sheets (.xls likely per .ods conversion) via python while maintaining all of the sheet's original content. Unlike xlutils (http://www.python-excel.org/) the iac-protocol (http://pythonhosted.org/iac-protocol/index.html) seems to me to be more fit/elegant tool to maintain sheet's style,formulas,dropboxes etc. One of the steps to launch iac's server or interpreter (iacs/iaci) is to initialize the interface which consists among others of this command:
import iac.app.libreoffice.calc as localc
While import iac.app.libreoffice works fine
moving to calc level
import iac.app.libreoffice.calc
throws following error
import iac.app.libreoffice.calc
Traceback (most recent call last):
File "", line 1, in
File "/usr/lib/python3.4/site-packages/iac/app/libreoffice/calc.py", line 11, in
from uno import getComponentContext
ImportError: cannot import name 'getComponentContext'
From what I've learned so far on this forum it might be linked to method name duplicity between two modules. This is where I am stuck. How do I learn which other module has such name of a method and how to fix it? Both iac-protocol and unotools are modules downloaded via pip3. I did not created method of such name in any script.
Thank you in advance for any advice!
Python3.4 on Scientific Linux release 7.3 (Nitrogen) LibreOffice 5.0.6.2 00(Build:2)
Some questions to narrow down the problem:
Did you start libreoffice listening on a socket first?
Did you import anything else before import iac.app.libreoffice.calc?
What happens when you start python in a terminal and enter from uno import getComponentContext?
I installed iac-protocol on Linux Mint and was able to import iac.app.libreoffice.calc and then use it. The installation process was complex, so I wouldn't be surprised if there is some problem with how your packages were installed, or possibly it does not work on RHEL-based systems. For one thing, it required me to install gnumeric.
The Calc "Hello World" code that worked for me is as follows.
libreoffice "--accept=socket,host=localhost,port=18100;urp;StarOffice.ServiceManager" --norestore --nofirststartwizard --nologo --calc &
python3
>>> import iac.app.libreoffice.calc as localc
>>> doc = localc.Interface.current_document()
>>> sheet = doc.getSheets().getByIndex(0)
>>> cell = sheet.getCellByPosition(0,0)
>>> cell.setString("Hello, World!")
One more thought: Have you considered using straight PyUNO starting from import uno instead of a wrapper library? That would avoid dependency on some of the extra libraries which may be causing the problem. Also there is better documentation for straight PyUNO.

Resources