Python - Using win32com.client to accept all changes in Word Documents - python

I'm using win32com.client from the pywin32 module to accept all tracked changes in a word document (Python 3.6.4 on Windows 10 64 bit).
Specifically the code I'm using is the following:
import win32com.client as win32
word = win32.gencache.EnsureDispatch("Word.Application")
word.Visible = False
doc = word.Documents.Open(PATH TO WORD FILE)
doc.Activate()
word.ActiveDocument.TrackRevisions = False # Maybe not need this
try:
word.WordBasic.AcceptAllChangesInDoc()
except TypeError:
pass
word.ActiveDocument.Save()
doc.Close(False)
word.Application.Quit()
I have two questions.
1.) Is there a better way to accept all changes rather than using the try-except block? Using this method produces a TypeError so a try-except block is required to finish the program.
2.) Do you know how you can delete comments left from users?

Here is a code working with Python 2.7 (I assume it works with Python 3.6.4 as well - I'm not familiar yet with the change between 2.X and 3.X)
#!/usr/bin/env python3
import win32com.client as win32
path_file_name = "YourPath\ToYour\doc.docx"
word = win32.gencache.EnsureDispatch("Word.Application")
word.Visible = False
doc = word.Documents.Open(path_file_name )
doc.Activate()
word.ActiveDocument.TrackRevisions = False # Maybe not need this (not really but why not)
# Accept all revisions
word.ActiveDocument.Revisions.AcceptAll()
# Delete all comments
if word.ActiveDocument.Comments.Count >= 1:
word.ActiveDocument.DeleteAllComments()
word.ActiveDocument.Save()
doc.Close(False)
word.Application.Quit()
Let me know if it works out for you.

Related

Rename a window

Iam trying to get the HWND of multiple windows called the same, and i thought the easy way would be to rename the one i found and search again, but it seems iam not allowed to rename it the way i want.
This is what i have tryed
import win32gui
import win32api
test = win32gui.FindWindow(0, "notepad")
win32gui.SetWindowText(test, "testname")
I have done this in my old project. may be this could help you. use this example as your reference
def UpdateControl_FromValue(self):
name_val = self.GetOptionValue(self.option_folder_name)
id_val = self.GetOptionValue()
self.in_setting_name = True
if id_val:
self.SetOptionValue("", self.option_folder_name)
opt_processors.FolderIDProcessor.UpdateControl_FromValue(self)
else:
if name_val:
win32gui.SetWindowText(self.GetControl(), name_val)
self.in_setting_name = False
FindWindow can return an invalid handle (eg. when no windows matching the text is found). That could be your issue.
The win32gui module exposes win32gui.EnumWindows (doc) that iterates top level windows. You can provide a custom callback that transform the window title.
In the following sample, I filter windows by title prefix, but you could implement a filter with regex, if that's more suitable for you.
import win32gui
def f(hwnd, more):
title = win32gui.GetWindowText(hwnd)
# print(f"{hwnd} - {title}")
prefix = 'notepad'
if title.startswith(prefix):
win32gui.SetWindowText(hwnd, title[len(prefix):])
win32gui.EnumWindows(f, None)

How to assign a 2d libreoffice calc named range to a python variable. Can do it in Libreoffice Basic

I can't seem to find a simple answer to the question. I have this successfully working in Libreoffice Basic:
NamedRange = ThisComponent.NamedRanges.getByName("transactions_detail")
RefCells = NamedRange.getReferredCells()
Set MainRange = RefCells.getDataArray()
Then I iterate over MainRange and pull out the rows I am interested in.
Can I do something similar in a python macro? Can I assign a 2d named range to a python variable or do I have to iterate over the range to assign the individual cells?
I am new to python but hope to convert my iteration intensive macro function to python in hopes of making it faster.
Any help would be much appreciated.
Thanks.
LibreOffice can be manipulated from Python with the library pyuno. The documentation of pyuno is unfortunately incomplete but going through this tutorial may help.
To get started:
Python-Uno, the library to communicate via Uno, is already in the LibreOffice Python’s path. To initialize your context, type the following lines in your python shell :
import socket # only needed on win32-OOo3.0.0
import uno
# get the uno component context from the PyUNO runtime
localContext = uno.getComponentContext()
# create the UnoUrlResolver
resolver = localContext.ServiceManager.createInstanceWithContext(
"com.sun.star.bridge.UnoUrlResolver", localContext )
# connect to the running office
ctx = resolver.resolve( "uno:socket,host=localhost,port=2002;urp;StarOffice.ComponentContext" )
smgr = ctx.ServiceManager
# get the central desktop object
desktop = smgr.createInstanceWithContext( "com.sun.star.frame.Desktop",ctx)
# access the current writer document
model = desktop.getCurrentComponent()
Then to get a named range and access the data as an array, you can use the following methods:
NamedRange = model.NamedRanges.getByName(“Test Name”)
MainRange = NamedRange.getDataArray()
However I am unsure that this will result in a noticeable preformance gain.

How can I switch between multiple open powerpoint presentations using python?

Judging by other posts such as Python Window Activation this might not be as simple as I was hoping, but I'm compelled to ask anyway.
In the snippet below, using win32com.client and Application.Presentations.Open(ppt1, ReadOnly=0) will open and activate a powerpoint presentation. Using Application.Presentations.Open(ppt2, ReadOnly=0) will open and activate another powerpoint presentation. As you will see, I can easily reference the former presentation and do SOME things with it, but it will NOT become the active window. How can this be accomplished?
Here's what I've been working with:
# The following requires two existing presentations
# ppt1.pptx and ppt2.pptx in a directory named C:\\pptTest\\
import win32com.client
Application = win32com.client.Dispatch("PowerPoint.Application")
directory = 'C:\\pptTest\\'
ppt_a = 'ppt_a.pptx'
ppt_b = 'ppt_b.pptx'
presentation_a = directory + ppt_a
presentation_b = directory + ppt_b
pres_a = Application.Presentations.Open(presentation_a, ReadOnly=0)
pres_b = Application.Presentations.Open(presentation_b, ReadOnly=0)
pres_a_slide1 = pres_a.Slides.Add(len(pres_a.Slides)+1, 12)
shape_a_1 = pres_a_slide1.Shapes.AddTextbox(Orientation=0x1,Left=100,Top=50,Width=400,Height=100)
shape_a_1.TextFrame.TextRange.Text='PRESENTATION A'
##%%
pres_b_slide1 = pres_b.Slides.Add(len(pres_b.Slides)+1, 12)
shape1 = pres_b_slide1.Shapes.AddTextbox(Orientation=0x1,Left=100,Top=50,Width=400,Height=100)
shape1.TextFrame.TextRange.Text='PRESENTATION B'
Result:
As you can see by the example, I can still reference the first presentation after opening another, but I can't make it the active window. The reason why I'm so interested in this, is that some methods will not work properly unless the presentation I'm editing through Python is also the active window. I'll get into those details as well if that is interesting to anyone.
Thank you for any suggestions!
Several examples, see if that works for you
import win32com.client as win32
file1='C:/test.pptm'
file2='C:/test2.pptm'
PowerPoint=win32.DispatchEx("PowerPoint.Application")
PowerPoint.Visible = True
Presentation1 = PowerPoint.Presentations.Open(file1)
Presentation2 = PowerPoint.Presentations.Open(file2)
PowerPoint.Windows(1).Activate()
PowerPoint.Windows(2).Activate()
print(PowerPoint.ActivePresentation.Name) # to see the name
PowerPoint.Presentations("test.pptm").Windows(1).Activate()
PowerPoint.Presentations("test2.pptm").Windows(1).Activate()
Presentation1.Windows(1).Activate()
Presentation2.Windows(1).Activate()
I dug through some old code and found something where I also had to switch between different open programs :
from win32com.client import Dispatch
autoit = Dispatch("AutoItX3.Control")
def _window_movement_windows(page_title):
autoit.WinSetOnTop(page_title, "", 1)
autoit.WinActivate(page_title, "")
autoit.WinWaitActive(page_title)
An example how to setup AutoIt with python can be found here : Calling AutoIt Functions in Python

How to read file capabilities using Python?

On Linux systems root privileges can be granted more selectively than adding the setuid bit using file capabilities. See capabilities(7) for details. These are attributes of files and can be read using the getcap program. How can these attributes be retrieved in Python?
Even though running the getcap program using e.g. subprocess for answering such a question is possible it is not desirable when retrieving very many capabilities.
It should be possible to devise a solution using ctypes. Are there alternatives to this approach or even libraries facilitating this task?
Python 3.3 comes with os.getxattr. If not, yeah... one way would be using ctypes, at least to get the raw stuff, or maybe use pyxattr
For pyxattr:
>>> import xattr
>>> xattr.listxattr("/bin/ping")
(u'security.capability',)
>>> xattr.getxattr("/bin/ping", "security.capability")
'\x00\x00\x00\x02\x00 \x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
For Python 3.3's version, it's essentially the same, just importing os, instead of xattr. ctypes is a bit more involved, though.
Now, we're getting the raw result, meaning that those two are most useful only retrieving textual attributes. But... we can use the same approach of getcap, through libcap itself:
import ctypes
libcap = ctypes.cdll.LoadLibrary("libcap.so")
cap_t = libcap.cap_get_file('/bin/ping')
libcap.cap_to_text.restype = ctypes.c_char_p
libcap.cap_to_text(cap_t, None)
which gives me:
'= cap_net_raw+p'
probably more useful for you.
PS: note that cap_to_text returns a malloced string. It's your job to deallocate it using cap_free
Hint about the "binary gibberish":
>>> import struct
>>> caps = '\x00\x00\x00\x02\x00 \x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
>>> struct.unpack("<IIIII", caps)
(33554432, 8192, 0, 0, 0)
In that 8192, the only active bit is the 13th. If you go to linux/capability.h, you'll see that CAP_NET_RAW is defined at 13.
Now, if you wan to write a module with all those constants, you can decode the info. But I'd say it's much more laborious than just using ctypes + libcap.
I tried the code from Ricardo Cárdenes's answer, but it did not work properly for me, because some details of the ctypes invocation incorrect. This issue caused a truncated path string to be passed to getxattr(...) inside of libcap, which thus returned the wrong capabilities list for the wrong item (the / directory, or other first path character, and not the actual path).
It is very important to remember and account for the difference between str and bytes in Python 3.X. This code works properly on Python 3.5/3.6:
#!/usr/bin/env python3
import ctypes
import os
import sys
# load shared library
libcap = ctypes.cdll.LoadLibrary('libcap.so')
class libcap_auto_c_char_p(ctypes.c_char_p):
def __del__(self):
libcap.cap_free(self)
# cap_t cap_get_file(const char *path_p)
libcap.cap_get_file.argtypes = [ctypes.c_char_p]
libcap.cap_get_file.restype = ctypes.c_void_p
# char* cap_to_text(cap_t caps, ssize_t *length_p)
libcap.cap_to_text.argtypes = [ctypes.c_void_p, ctypes.c_void_p]
libcap.cap_to_text.restype = libcap_auto_c_char_p
def cap_get_file(path):
cap_t = libcap.cap_get_file(path.encode('utf-8'))
if cap_t is None:
return ''
else:
return libcap.cap_to_text(cap_t, None).value.decode('utf-8')
print(cap_get_file('/usr/bin/traceroute6.iputils'))
print(cap_get_file('/usr/bin/systemd-detect-virt'))
print(cap_get_file('/usr/bin/mtr'))
print(cap_get_file('/usr/bin/tar'))
print(cap_get_file('/usr/bin/bogus'))
The output will look like this (anything nonexistent, or with no capabilities set just returns '':
= cap_net_raw+ep
= cap_dac_override,cap_sys_ptrace+ep
= cap_net_raw+ep

Adding testcase results to Quality Center Run from a outside Python Script

I want to try to add all the step details - Expected, Actual, Status,
etc. to a QC Run for a testcase of a TestSet from a Python Script
living outside the Quality Center.
I have come till here (code given below) and I don't know how to add
Step Expected and Step Actual Result. If anyone knows how do it,
please help me out!! Please, I don't want any QTP solutions.
Thanks,
Code-
# Script name - add_tsrun.py
# C:\Python27\python.exe
# This script lives locally on a Windows machine that has - Python 2.7, Win32 installed, IE8
# Dependencies on Windows Machine - Python 2.7, PythonWin32 installed, IE8, a QC Account, connectivity to QCServer
import win32com.client, os
tdc = win32com.client.Dispatch("TDApiOle80.TDConnection")
tdc.InitConnection('http://QCSERVER:8080/qcbin')
tdc.Login('USERNAME', 'PASSWORD')
tdc.Connect('DOMAIN_NAME', 'PROJECT')
tsFolder = tdc.TestSetTreeManager.NodeByPath('Root\\test_me\\sub_folder')
tsList = tsFolder.FindTestSets('testset1')
ts_object = tsList.Item(1)
ts_dir = os.path.dirname('testset1')
ts_name = os.path.basename('testset1')
tsFolder = tdc.TestSetTreeManager.NodeByPath(ts_dir)
tsList = tsFolder.FindTestSets(ts_name)
ts_object = tsList.Item(1)
TSTestFact = ts_object.TSTestFactory
TestSetTestsList = TSTestFact.NewList("")
ts_instance = TestSetTestsList.Item(1)
newItem = ts_instance.RunFactory.AddItem(None) # newItem == Run Object
newItem.Status = 'No Run'
newItem.Name = 'Run 03'
newItem.Post()
newItem.CopyDesignSteps() # Copy Design Steps
newItem.Post()
steps = newItem.StepFactory.NewList("")
step1 = steps[0]
step1.Status = "Not Completed"
step1.post()
## How do I change the Actual Result??
## I can access the Actual, Expected Result by doing this, but not change it
step1.Field('ST_ACTUAL') = 'My actual result' # This works in VB, not python as its a Syntax error!!
Traceback ( File "<interactive input>", line 1
SyntaxError: can't assign to function call
Hope this helps you guys out there. If you know the answer to set the
Actual Result, please help me out and let me know. Thanks,
Amit
As Ethan Furman answered in your previous question:
In Python () represent calls to functions, while [] represent indexing and mapping.
So in other words, you probably want to do step1.Field['ST_ACTUAL'] = 'My actual result'
Found the answer after a lot of Google Search :)
Simple -> Just do this:
step1.SetField("ST_ACTUAL", "my actual result") # Wohhooooo!!!!
If the above code fails to work, try to do the following:-
(OPTIONAL) Set your win32 com as follows- (Making ''Late Binding'')
# http://oreilly.com/catalog/pythonwin32/chapter/ch12.html
a. Start PythonWin, and from the Tools menu, select the item COM Makepy utility.
b. Using Windows Explorer, locate the client subdirectory (OTA COM Type Library)
under the main win32com directory and double-click the file makepy.py.
Thank you all...

Categories

Resources