Python Forum
wrap_text with openpyxl. How to use documentation to resolve deprecation warning?
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?
Reply
#2
because they advise you to use copy.copy()

look also https://stackoverflow.com/questions/2333...l/34838233
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#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.
Reply
#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()
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#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!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  openpyxl documentation OldMainframeProgrammer 3 545 Dec-20-2023, 08:39 AM
Last Post: buran
  how to avoid deprecation notice merrittr 5 986 Nov-27-2023, 11:12 PM
Last Post: rob101
  PyPDF2 deprecation problem gowb0w 5 3,523 Sep-21-2023, 12:38 PM
Last Post: Pedroski55
  [split] How to resolve version conflicts in Python? atonalwilson 1 949 May-04-2023, 09:02 AM
Last Post: buran
  How to resolve version conflicts in Python? taeefnajib 0 872 Apr-27-2023, 08:37 PM
Last Post: taeefnajib
  How to resolve my problem in Pycharm? bshoushtarian 0 821 Sep-26-2022, 11:45 AM
Last Post: bshoushtarian
  Solving equation equal to zero: How to resolve the syntax error? alexfrol86 3 1,892 Feb-21-2022, 08:58 AM
Last Post: deanhystad
  win32com — How to resolve “AttributeError: xlUp” for Excel files? JaneTan 2 4,124 Aug-18-2021, 05:27 AM
Last Post: snippsat
  How to resolve Index Error in my code? codify110 6 2,956 May-22-2021, 11:04 AM
Last Post: supuflounder
  How to resolve numpy ValueError: dtype.descr Py_veeran 0 1,815 Aug-18-2020, 06:46 PM
Last Post: Py_veeran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020