Python Forum
Paste Special with multiple arguments with Dispatch
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Paste Special with multiple arguments with Dispatch
#1
Hi!

I am trying to change some cells in excel with python. There is an error that sometimes occures in column K. Basically the "The number in this cell is formatted as text or preceded by an apostrophe". In excel you just click on the error and the click on convert to number to get rid of it.

You can't do this in Python but I figured out a way around it. If you put "1" in a cell and copy it, and the used Pastespecial on the column K with Values and Multiply as operation it fixes it. I can almost do this in Python but can't find the correct syntax for it.

sheet['Z1'] = 1
from win32com.client import Dispatch

excel = Dispatch("Excel.Application")
excel.Visible = 1
source = excel.Workbooks.Open(mainworkingdirectory + "\\XYZ.xlsm")
excel.Range("Z1").Select()
excel.Selection.Copy()
excel.Range("K:K").Select()
excel.Selection.PasteSpecial(Paste = -4163, Operation = 4)
The last line is wrong. I got the two numbers from here, -4163 stands for the "Values" and 4 is for the "Multiply":
https://analysistabs.com/vba-code/range/m/pastespecial/

It runs fine, but the problem still stands. What am i doing wrong, is there another way around this?
Reply
#2
can't you just cast the value as int?
int(value)
Reply
#3
It's inside the excel file and I need to make the conversion in excel with the tools of excel via Python. Just converting the via "int()" did not work. Or do you mean it in another way?
Reply
#4
Quote:Or do you mean it in another way?
No, I thought this was a python extract.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  What script to paste folders thenewcoder 1 676 Nov-29-2023, 09:40 AM
Last Post: Pedroski55
  Please help me [copy and paste file from src to dst] midomarc 2 1,023 Nov-24-2022, 10:13 PM
Last Post: midomarc
Shocked Help for COM server implementation ("Unspecified error" on Dispatch()) Alfalfa 2 3,486 Jul-25-2021, 01:36 AM
Last Post: Alfalfa
  ImageTk Paste KDog 14 6,953 Jun-27-2021, 11:07 AM
Last Post: KDog
  Rename Multiple files in directory to remove special characters nyawadasi 9 6,413 Feb-16-2021, 09:49 PM
Last Post: BashBedlam
  Cut and Paste Oshadha 3 2,445 Jan-20-2021, 04:27 PM
Last Post: spaceraiders
  How to pass multiple arguments into function Mekala 4 2,463 Jul-11-2020, 07:03 AM
Last Post: Mekala
  Using function *args to multiply multiple arguments allusernametaken 8 6,100 Nov-20-2019, 12:01 AM
Last Post: allusernametaken
  copy paste file and re-name it asheru93 1 2,382 May-24-2019, 10:43 AM
Last Post: heiner55
  Giving class multiple arguments AndyArsalan 1 4,565 Oct-04-2018, 11:25 PM
Last Post: ODIS

Forum Jump:

User Panel Messages

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