Formatting Image in Excel using win32com.client - image

I am creating an excel spreadsheet using pythons win32com module excel client. I wanted to add a logo to my excel spreadsheet report. So far I have managed to add the picture:
# Set a variable to an empty excel instance
excel = win32com.client.Dispatch("Excel.Application")
# Initialize a workbook within excel
book = excel.Workbooks.Add()
# Create sheet in book
sheet = book.Worksheets(1)
sheet.Pictures().Insert(r"G:\logos\Logo.jpg")
I've been pouring through the web and I cannot seem to find a way to access the position properties of the picture to move to it a particular place, nor can I find out how to access the sizing properties. Is there a help doc out there that has some examples that I cannot seem to find?

Try
cell = sheet.Cells(1,1)
pic = sheet.Pictures().Insert(r"G:\logos\Logo.jpg")
pic.Left = cell.Left + 20
pic.Top = cell.Top + 30
which will position your picture at 20 pixels right and 30 down from top left corner of given cell.
Regarding help, my reference is search for "excel interop " such as "excel interop range" or "excel interop picture" which leads to Picture object docs.

Related

Variable Increase in MS PowerPoint

I've very limited knowledge of PowerPoint but have been asked to create a presentation slide where I would have to click areas on a map and that would change color and increment a number (population). I am able to change the color of the map on click and I'm able to display a bubble but I am not able to increment the population number based on the areas of the map that are clicked.
I've looked at some solutions around here and did some research online but couldn't find anything that would point me in the right direction.
Is there a way to create a variable and pass a number when you click a specific area of the map, which is a Freeform.
Thank you in advance for the help.
You can set each shape's Action Setting to Run Macro: (macro name)
That will run the VBA subroutine called (macro name) when you click on the shape during a slide show.
Mac PowerPoint has a few bugs that keep this from working as smoothly as it does in Windows PowerPoint but the code below works around these issues and works in either version.
Sub IncrementNumber(oSh as Shape)
Dim oSl as Slide
Dim oShTemp as Shape
' oSh.Parent returns a valid reference to the shape's host slide:
Set oSl = ActivePresentation.Slides(oSh.Parent.SlideIndex)
' and oSh.Name works as well
' So we use those two bits to get a reference
' to the clicked shape like so
Set oShTemp = oSl.Shapes(oSh.Name)
With oShTemp
.TextFrame.TextRange.Text = _
Cstr(CLng(oShTemp.TextRange.Text) + 1)
End With
End Sub
About this bit:
Cstr(CLng(oShTemp.TextRange.Text) + 1)
This gets the current text in the shape, converts it from text (string) to a number so we can do arithmetic on it, adds 1 to it, then converts it back to text so we can plug it back into the shape's text.
If you don't know what to do with VBA code examples, there's a tutorial on the PowerPoint FAQ site that I maintain:
How do I use VBA code in PowerPoint
http://www.pptfaq.com/FAQ00033_How_do_I_use_VBA_code_in_PowerPoint.htm

Excel: create image from cell range

Using Excel VBA, I want to create an image showing the content of a cell.
Manually, I can do this by selecting a cell, clicking on 'copy', selecting another cell, and clicking on 'paste as image'. Then I can select the image and set its formula to =$B$2, for example.
The Macro recorder tells me that this is equivalent to the following code:
Selection.Copy
ActiveSheet.Pictures.Paste.Select
ActiveCell.FormulaR1C1 = "=R[1]C[1]"
I want to achieve the same without using the copy-paste-select commands. In other words, I would like to create a Picture object with predefined FormulaR1C1 property.
I tried ActiveSheet.Pictures.Add(Left, Top, Width, Height) but only got Runtime error '1004': No link to paste. I don't understand what this means.
I tried ActiveSheet.Pictures.Insert and ActiveSheet.Shapes.AddPicture, but they both require a file name to load an external image file, which is not what I want.
Instead of copying the cell, then telling it you want to paste to another cell as a picture, try using:
Range("A1").CopyPicture Appearance:=xlScreen, Format:=xlPicture
Now A1 is in your clipboard as a picture. You can use boring old paste to stick it somewhere else.
Range("B1").Select
ActiveSheet.Paste
Are you trying to get a "live" picture which is linked back to the source range, like using the Camera tool in Excel?
Sub Macro1()
Dim s, rng As Range, rngDest As Range
Set rng = ActiveSheet.Range("B2:C3")
Set rngDest = ActiveSheet.Range("E10")
rng.Copy
With ActiveSheet.Pictures.Paste(link:=True)
.Left = rngDest.Left
.Top = rngDest.Top
End With
End Sub
I don't think you can get around using copy/paste though.

Ruby Script to color all sheets present in excel with a particular colour

I need a ruby script to color all the sheets present in excel workbook with a fixed colour...all at once....
here i need to color this sheet with yellow color but the coloring should be only for cells with filled data.Something lik ethis
You need to check the MSDN documentation of UsedRange Property. Also see the 56 Excel ColorIndex Colors to verify the color index of yellow. Look this also Change cell background color using VBA .
I use the Excel sheet as below :
Here is the code :
require 'win32ole'
# create an instance of the Excel application object
excel = WIN32OLE.new('Excel.Application')
# make Excel visible
excel.visible = true
# open the excel from the desired path
wb=excel.workbooks.open("C:\\Users\\test.xlsx")
# get the first Worksheet
wbs= wb.worksheets(1)
# fill the column with a specific coloe
wbs.usedrange.interior.colorindex = 6

Is it possible to use VBA to ungroup an EPS picture to create a Microsoft Office drawing object in Word?

Ungrouping an EPS picture, converting it to a Microsoft Office drawing object in the process, is possible in PowerPoint as follows:
oShp.Ungroup
and Excel like this:
Selection.ShapeRange.Ungroup
But the closest I can get in Word (with the non-inline shape selected in the document) is this:
ActiveWindow.Selection.ShapeRange(1).Ungroup
and this returns the error "Run-time error '-2147024891 (80070005)': Group is locked and cannot be ungrouped".
Yet when I right click on the EPS picture in the document and select Ungroup, I can convert it to a Microsoft Office drawing object and then ungroup it once more to get the individual shapes.
There must be a way to do this in VBA?
You need to check if the shape is a 'Group'
Try this.
Sub Sample()
Dim shp As Shape
For Each shp In ActiveDocument.Shapes
If shp.Type = msoGroup Then shp.Ungroup
Next
End Sub

how to extract list or bullet details from .pptx or .ppt file using c#

I am using Microsoft office Interope assembly to extract features from .pptx and .ppt files using c#. I was able to extract details about shapes, animations but not able to extract details about which types of bullets ppt or pptx consists of or which slide contains bullets etc.
Please help me finding this.
Thanks in advance.
In VBA, you'd do something like this to examine the bullets on slide 1
Dim oSh As Shape
Dim x As Long ' Integer in C#?
For Each oSh In ActivePresentation.Slides(1).Shapes
With oSh
If .HasTextFrame Then
If .TextFrame.HasText Then
With .TextFrame2.TextRange
For x = 1 To .Paragraphs.Count
Debug.Print .Paragraphs(x).ParagraphFormat.Bullet.[Various properties]
Next
End With
End If
End If
End With
Next
Look at the code in the PPT VBA editor. When you type the dot after Bullet above, intellisense will show you the available properties.
There are a few ways. In the code below, you can see the properties of the text that you can access in your program:
ppTextBox.TextFrame2.TextRange.ParagraphFormat.Bullet.Type =
Office.MsoBulletType.msoBulletNumbered;
ppTextBox.TextFrame2.TextRange.ParagraphFormat.Bullet.Style =
Office.MsoNumberedBulletStyle.msoBulletAlphaLCParenBoth;
ppTextBox.TextFrame2.TextRange.ParagraphFormat.Bullet.StartValue = 4;
ppTextBox.TextFrame2.TextRange.ParagraphFormat.Bullet.UseTextColor =
Office.MsoTriState.msoTrue;
ppTextBox.TextFrame2.TextRange.ParagraphFormat.Bullet.UseTextFont =
Office.MsoTriState.msoTrue;
where ppTextBox is a shape object, and note the use of TextFrame2 rather than TextFrame. You can interrogate the ParagraphFormat.Bullt.Type against the enumerated list Office.MsoBulletType to see which has been applied.
For more details, check this page for more details on text processing in powerpoint with C#.

Resources