-1

As the title suggested, I've tried to get the last row so I can determine the end of the range I need to copy for pasting into Outlook. I had to use win32com because it will allow me to pull the data from an opened Excel book while Pandas or openxyl doesn't allow me to do that. Below is the snippet of code I tried to do.

I did try to use sheet.max_row or even len(sheet['G') to get last row but it doesn't allow enumeration apparently.

import os
import win32com.client as client
from PIL import ImageGrab
from datetime import date
from openpyxl import load_workbook

(...)

excel = client.GetActiveObject('Excel.Application')

wb = excel.Workbooks('Test Work')

sheet = wb.Sheets['PIVOT TABLE']

loc = str(sheet.Range('B5'))

locEmail = loc_EmailName(loc.split(',')[0])

copyrange = sheet.Range('A4:G11')
copyrange.CopyPicture(Appearance=1, Format=2)

(...)
CptLuna
  • 45
  • 5
  • 1
    https://stackoverflow.com/q/26772346 – BigBen Feb 02 '23 at 21:00
  • I've tried these solutions on the link you posted and it doesn't work. That is 8 years old and completely out of date. Super unhelpful. – CptLuna Feb 03 '23 at 12:32
  • 1
    https://stackoverflow.com/a/57358333 specifically. Using the constant `xlUp` (as long as it's properly referred to`) is *not* out of date. – BigBen Feb 03 '23 at 13:25
  • 1
    @BigBen thank you, I finally bludgeon that specific excel wrapper to work for me. Definitely current! :) – CptLuna Feb 07 '23 at 15:01

1 Answers1

0
def get_column_after(self, column, offset):
        global counter
        for item in self.ws.Range("{0}{1}:{0}{2}".format(column, offset, self.get_last_row_from_column(column))).Value:
            print(item[0])
            counter = counter + 1

I simply added a counter so that I could get the final row number and used that to augment my range of what to copy. Thank you to @BigBen for the pointer to the answer from how to find if last row has value for a specific column in Excel using Python Win32com.

CptLuna
  • 45
  • 5