Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 wrap_text with openpyxl. How to use documentation to resolve deprecation warning?
#1
Hi All,

I run the following openpyxl command to wrap text in all rows after row 9. It works fine but throws a deprecation warning. I'd love to figure out how to use documentation such as https://openpyxl.readthedocs.io/en/stable/ to determine the current, non-deprecated, way to wrap_text. But I always find the documentation confusing and unhelpful. For example, if I search for wrap_text I get this: https://openpyxl.readthedocs.io/en/stabl...t.wrapText

But that tells me nothing about how to wrap text. Do I simply not know how to use the documentation? Is there some great mystery I am to unravel so I don't have to endlessly google for examples on how to use openpyxl? How does one look at such documentation and figure out out how to wrap_text in a cell?

Here is the code:
from openpyxl import load_workbook
from openpyxl.styles import Alignment

file1 = "C:\\folder\\inputFile1.xlsx"
wb=load_workbook(file1)
ws = wb.active
for rows in ws.iter_rows(min_row=10, max_row=None, min_col=None, max_col=None):
    for cell in rows:
       cell.alignment =  cell.alignment.copy(wrapText=True)
    wb.save('C:\\folder\file1_wrap.xlsx')    
here is the deprecation warning:
C:\Users\Jcurran\AppData\Local\Continuum\anaconda3\lib\site-packages\ipykernel_launcher.py:10: DeprecationWarning: Call to deprecated function copy (Use copy(obj) or cell.obj = cell.obj + other).
# Remove the CWD from sys.path while we load stuff.

How might I figure out the way to find the information required to use the current (non-deprecated) approach to wrapping text in cells via the documentation at https://openpyxl.readthedocs.io/en/stable/?

I am using Jupyter for my environment. Shift tab or tab doesn't give me anything useful.

Any suggestions? I crave self sufficiency but can't grasp how to navigate the documentation for answer. There must be some clue somewhere? Some source code perhaps that I do not know how to locate?
Quote
#2
because they advise you to use copy.copy()

look also https://stackoverflow.com/questions/2333...l/34838233
Quote
#3
Through sheer trial and error I got this to work. But I'd like to understand how I can approach without trial and error:

from openpyxl import load_workbook
from openpyxl.styles import Alignment

HISorigFile = "C:\\Users\\Jcurran\\OneDrive - Wolters Kluwer\\Desktop\\Desktop\\weekly reports draft\\Inpatient HIS Use by Vendor Live Only Landscape Margins Width Freeze Filter.xlsx"
wb=load_workbook(HISorigFile)
ws = wb.active
for rows in ws.iter_rows(min_row=10, max_row=None, min_col=None, max_col=None):
    for cell in rows:
        cell.alignment = Alignment(wrapText=True)

Buran,

OK. So I should not be looking at openpyxl docs I should be looking at python docs for copy and deprecation I guess. I am trying to understand how you knew to look there.

the deprecation warning says:

(Use copy(obj) or cell.obj = cell.obj + other).

But I don't understand how you took that warning and then determined that copy.copy is the new approach.

Of course through trial and error I got this to work with no copy or copy.copy.

cell.alignment = Alignment(wrapText=True)
but this does not work:

cell.alignment =  cell.alignment.copy.copy(wrapText=True)
as it gives this:

AttributeError: 'function' object has no attribute 'copy'


So I still don't know how one approaches this through documentation and not rely on trial and error.
Quote
#4
(Oct-09-2019, 11:54 AM)curranjohn46 Wrote: I am trying to understand how you knew to look there.
simply by reading the warning:
Use copy(obj) or cell.obj = cell.obj + other)

if you are still uncertain you can look also at the docs and source code
https://openpyxl.readthedocs.io/en/stabl...Proxy.copy
https://openpyxl.readthedocs.io/en/stabl...StyleProxy

in the source you can see copy() method is more or less wrapper around copy.copy()
Quote
#5
I learned that I did not have the latest openpyxl version. pip install openpyxl installed 2.5. I upgraded it to 3.0.
Now when I look at https://openpyxl.readthedocs.io/en/stable/index.html it makes more sense :-)

I now know that the "Working with Styles" section of the openpyxl 3.0 documentation is the place to go for formatting data.

So I click that, and go to https://openpyxl.readthedocs.io/en/stable/styles.html

That page show me this:

>>> alignment=Alignment(horizontal='general',
... vertical='bottom',
... text_rotation=0,
... wrap_text=False,
... shrink_to_fit=False,
... indent=0)

and I could use that info to wrap text with this line:
cell.alignment = Alignment(wrapText=True)
Now things are starting to make sense for me. :-) Thanks!
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  How to resolve 404 returned from web method when not running on localhost littleGreenDude 3 441 Feb-05-2019, 09:01 PM
Last Post: littleGreenDude
  function wanted: resolve relative path Skaperen 4 584 Sep-06-2018, 01:52 AM
Last Post: Skaperen

Forum Jump:


Users browsing this thread: 1 Guest(s)