AttributeError: <unknown>.Range - python

I'm trying to autofill with "fill series" formatting the value of cell A11 into A12 on two worksheets. This needs to be achieved using win32com module. My code is:
from win32com.client import Dispatch
from win32com.client import constants
xl = Dispatch('Excel.Application')
xl.Visible = True
wb = xl.Workbooks.Open ('S:\\Height Peak.xls')
ws = wb.Worksheets(['Sheet1','Sheet2'])
ws.Select()
ws.Range('A10:A11').AutoFill(ws.Range('A11:A12'), xlFillSeries)
As soon as I run the code, I'm encountering the following error:
AttributeError: unknown.Range

There were 3 Problems:
1) You need to iterate over your worksheets!
2) The source Range
needs to be a subrange of the fill Range. That is not documented well
and I basically just figured that out from looking at examples in the
docs.
3) You import constants, but you need to actually specify your
constants' source! (see below
Code:
from win32com.client import Dispatch
from win32com.client import constants as const
xl = Dispatch('Excel.Application')
xl.Visible = True
wb = xl.Workbooks.Open ('S:\\Height Peak.xls')
ws = wb.Worksheets
for sheet in ws:
if sheet.Name.endswith("1") or sheet.Name.endswith("2"):
sourceRange = sheet.Range('A1:A10')
fillRange = sheet.Range('A1:A12')
sourceRange.AutoFill(fillRange, const.xlFillSeries)

Related

Is there a way to you can copy a range in excel and paste it in a specific slide in powerpoint using python win32/openpyxl/xlsxwriter/pptx

powerpoint_object = win32com.client.Dispatch("Powerpoint.Application")
powerpoint_object.visible = True
powerpoint_presentation = powerpoint_object.Presentations.Open(r"C:\Users\<User>\Desktop\test.pptx")
excel_object = win32com.client.Dispatch("Excel.Application")
excel_object.visible = True
excel_workbook = excel_object.Workbooks.Open(Filename=r"C:\Users\Desktop\X_2022-06-15.xlsx")
excel_worksheet = excel_workbook.Worksheets("Shee1")
excel_range = excel_worksheet.Range("A1:J50")
excel_range.Copy()
powerpoint_slide = powerpoint_presentation.Slides.Add(1,12)
powerpoint_slide.Shapes().Paste()
It is giving me the following error
pywintypes.com_error: (-2147352562, 'Invalid number of parameters.', None, None)
Am I going about this incorrectly or am I missing something? If not by using win32, is there a way to use openpyxl and pptx to do this?

python How to zoom sheet in xlwings

I need to use zoom in only xlwings, below code is not worked.
Plz help me....
import xlwings as xw
xw.App(visible=True)
workbook = xw.Book(r'zoom.xlsx')
sheet = workbook.sheets.active
# sheet.api.ActiveWindow.Zoom = 100
# sheet.app.api.active_window.zoom.set(100)
# excel.ActiveWindow.Zoom = 80
# sheet.api.PageSetup.Zoom = 100
import xlwings as xw
app = xw.apps.active
app.api.ActiveWindow.DisplayGridlines = False
app.api.ActiveWindow.Zoom = 80

Copying and Pasting from one Google Sheet to another

I am relatively new to python and am struggling to figure out a way to copy and paste data from one google sheet to another using gspread. Does anyone know how to do this without using win32 to copy to an excel as a bridge?? Please see the code and error msg below:
import gspread
from oauth2client.service_account import ServiceAccountCredentials
import pandas as pd
import numpy as np
Scope = ["https://spreadsheets.google.com/feeds",'https://www.googleapis.com/auth/spreadsheets',"https://www.googleapis.com/auth/drive.file","https://www.googleapis.com/auth/drive"]
creds = ServiceAccountCredentials.from_json_keyfile_name(r'C:\Users\Documents\Scripts\FX Rates Query\key.json', Scope)
client = gspread.authorize(creds)
sheet = client.open("Capital").sheet1
data=sheet.get_all_records()
df = pd.DataFrame(data)
df.to_excel(r'C:\Users\Documents\Reserves_extract.xlsx')
sheet1 = client.open("Cash Duration ").sheet1
mgnt_fees = sheet1.col_values(5)
fees = pd.DataFrame(mgnt_fees)
fees1 = fees[fees!=0]
print(fees1)
update = sheet1.update('B7',fees1)
##^^ERROR MSG IS COMING FROM HERE
Error msg:
raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type DataFrame is not JSON serializable
From your replying of I would like to copy a specific column from google spreadsheet A to google spreadsheet B, in this case, how about the following modification?
Modified script:
import gspread
from oauth2client.service_account import ServiceAccountCredentials
import pandas as pd
import numpy as np
Scope = ["https://spreadsheets.google.com/feeds",'https://www.googleapis.com/auth/spreadsheets',"https://www.googleapis.com/auth/drive.file","https://www.googleapis.com/auth/drive"]
creds = ServiceAccountCredentials.from_json_keyfile_name(r'C:\Users\Documents\Scripts\FX Rates Query\key.json', Scope)
client = gspread.authorize(creds)
# I modified below script.
spreadsheetA = client.open("Capital")
spreadsheetB = client.open("Cash Duration ")
srcCol = "'" + spreadsheetA.sheet1.title + "'!A1:A" # This is the column "A" of the 1st tab of Spreadsheet A.
dstCol = "'" + spreadsheetB.sheet1.title + "'!B1:B" # This is the column "B" of the 1st tab of Spreadsheet B.
src = spreadsheetA.values_get(srcCol)
del src['range']
spreadsheetB.values_update(dstCol, params={'valueInputOption': 'USER_ENTERED'}, body=src)
In this modified script, column "A" of the 1st tab of Spreadsheet A is copied to column "B" of the 1st tab of Spreadsheet B. Please modify this for your actual situation.
References:
values_get
values_update

Is any way the function taking the values from the imported entries from the the Tk?

So I want to run the Tk() and import there some values. Then these values will be checked from a function that will replacing the value in a particular cell in excel. This cell that matches with one imported entry. My challenge here is that the function works and replacing if I put manually the values inside the program BUT how can I make to read the values from the Tk() that I enter? I made a button so the function will be running after I have imported the values in the entry fields but still not. The "sntake" and "sngive" in the replace() function seems to not working... What am I missing?
Code:
from tkinter import *
import pandas as pd
from xlutils.copy import copy
from openpyxl import *
from openpyxl.utils.cell import get_column_letter
import openpyxl
app = Tk()
app.geometry("500x500")
app.title("S/N Management")
heading = Label(text="S/N Management",fg="black",bg="green",width="500",height="3",font="10")
heading.pack()
sngive_text = Label(text="S/N of the delivered ")
sntake_text = Label(text="S/N of the recieved ")
sngive_text.place(x=15,y=80)
sntake_text.place(x=15,y=160)
sngive = StringVar()
sntake = StringVar()
sngive_entry = Entry(textvariable=sngive,width="30")
sntake_entry = Entry(textvariable=sntake,width="30")
sngive_entry.place(x=15,y=100)
sntake_entry.place(x=15,y=180)
def replace():
wb = openpyxl.load_workbook('Tracker.xlsx')
wb.sheetnames
sheet = wb["serials"]
amountOfRows = sheet.max_row
amountOfColumns = sheet.max_column
for i in range(amountOfColumns):
for k in range(amountOfRows):
cell = str(sheet[get_column_letter(i+1)+str(k+1)].value)
if( str(cell) == sntake):
newCell = sngive
sheet[get_column_letter(i+1)+str(k+1)]=newCell
wb.save('tracker_updated.xlsx')
button = Button(app,text="Submit Data",command=replace,width="30",height="2",bg="grey")
button.place(x=140,y=420)
mainloop()

How to set title to a powerpoint slide using win32com client

Could anyone please help me in setting a title to a powerpoint slide using win32com library in Python. The following is the code. I have used the slide layout 11 which denotes Title only
import openpyxl as op
import pptx
import os
import win32com.client
import smtplib
os.chdir(r'C:\Users\aju.mathew.thomas\Desktop\PBC\Pepsi\PBC\Performance Reports\2019\PPT')
path= r'C:\Users\aju.mathew.thomas\Desktop\PBC\Pepsi\PBC\Performance Reports\2019\PPT\Summary2.xlsx'
wb = op.load_workbook(path)
ExcelApp = win32com.client.Dispatch("Excel.Application")
ExcelApp.Visible = False
workbook = ExcelApp.Workbooks.open(r'C:\Users\aju.mathew.thomas\Desktop\PBC\Pepsi\PBC\Performance Reports\2019\PPT\Summary2.xlsx')
worksheet = workbook.Worksheets("Summary")
excelrange = worksheet.Range("A2:R24")
PptApp = win32com.client.Dispatch("Powerpoint.Application")
PptApp.Visible = True
z= excelrange.Copy()
PPtPresentation = PptApp.Presentations.Open(r'C:\Users\aju.mathew.thomas\Desktop\PBC\Pepsi\PBC\Performance Reports\2019\PPT\PBC Performance Update.pptx')
pptSlide = PPtPresentation.Slides.Add(1,11)
#pptSlide.Title.Characters.Text ='Metrics'
#title = pptSlide.Shapes.Title
#title.Text ='Metrics Summary'
pptSlide.Shapes.PasteSpecial(z)
PPtPresentation.Save()
Just a small syntax issue:
PptApp = win32com.client.Dispatch("Powerpoint.Application")
PptApp.Visible = True
z= excelrange.Copy()
PPtPresentation = PptApp.Presentations.Open(r'C:\Users\aju.mathew.thomas\Desktop\PBC\Pepsi\PBC\Performance Reports\2019\PPT\PBC Performance Update.pptx')
pptSlide = PPtPresentation.Slides.Add(1,11)
title = pptSlide.Shapes.Title
title.TextFrame.TextRange.Text = 'My title here'

Categories

Resources