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'
Related
I want to change the voice of azure from python, with these characteristics
languageCode = 'es‑MX'
ssmlGender = 'FEMALE'
voicName = 'es‑MX‑DaliaNeural'
but i'm new to azure so i don't know how, this is my code:
import PyPDF2
import azure.cognitiveservices.speech as sdk
key = "fake key"
region = "fake region"
config = sdk.SpeechConfig(subscription=key, region=region)
synthesizer = sdk.SpeechSynthesizer(speech_config=config)
book = open("prueba.pdf", "rb")
reader = PyPDF2.PdfFileReader(book)
for num in range(0,reader.numPages):
text = reader.getPage(num).extractText()
result = synthesizer.speak_text_async(text).get()
Acording to the documentation https://learn.microsoft.com/en-us/azure/cognitive-services/speech-service/how-to-speech-synthesis?tabs=browserjs%2Cterminal&pivots=programming-language-python#select-synthesis-language-and-voice you should be able to do:
config.speech_synthesis_language = "es‑MX"
config.speech_synthesis_voice_name ="es-MX-DaliaNeural"
The list of voices is here https://learn.microsoft.com/en-us/azure/cognitive-services/speech-service/language-support?tabs=tts
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
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)
I can't figure this one out. It's complaining about the wb.save() line. I have no idea what's causing it after banging my head on this. I suspect it has something to do with trying to open a blank sheet and saving it after doing stuff with formatting, but I can't imagine what I'm doing there that's causing this problem. It worked fine when I opened an existing spreadsheet and did manipulations, but that required me to have an existing spreadsheet in the first place. Here, I'm trying to start a new spreadsheet from scratch.
from bs4 import BeautifulSoup
from lxml import etree
import os, codecs
import imageFilesSub
import re
import openpyxl, lxml
from openpyxl.utils import get_column_letter, column_index_from_string
homeEnv = 0 # 1 - home, 0 - work
if homeEnv:
filesDir = r'K:\Users\Johnny\My Documents\_World_of_Waterfalls\Website\tier 2 pages\tier 3 pages\tier 4 pages'
filesOutDir = r'K:\Users\Johnny\My Documents\_World_of_Waterfalls\WordPressSite'
else:
filesDir = r'..\old_travelblog_writeups'
filesOutDir = r'./'
# First get the list of files to parse
filesInDir = os.listdir(filesDir)
filesToParse = []
for file in filesInDir:
if ('travel-blog' in file) and (file.endswith('-template.html')):
filesToParse.append(file)
# Open up the travelBlog spreadsheet and set it up
wb = openpyxl.Workbook()
sheet = wb.active
sheet.name = "travelBlog List"
sheet['A1'].value = 'Blog No.'
sheet['B1'].value = 'Title'
sheet['C1'].value = 'Category'
sheet['D1'].value = 'Keyword Tags'
sheet['E1'].value = 'Excerpt'
sheet['F1'].value = 'Featured Image Filename'
sheet['G1'].value = 'Featured Image Alt Text'
sheet['H1'].value = 'Start Date'
sheet['I1'].value = 'End Date'
sheet['J1'].value = 'Old Web Address'
sheet['K1'].value = 'New Travel Blog Body Filename'
sheet['L1'].value = 'Old Travel Blog Template to parse'
sheet.freeze_panes = 'C2'
sheet.column_dimensions['A'].width = 10
sheet.column_dimensions['H','I'] = 20
sheet.column_dimensions['B','F','J','K','L'] = 40
sheet.column_dimensions['D','E'] = 50
from openpyxl.styles import Font
headerFontObj = Font(name='Arial', bold=True)
for col in range(1,sheet.max_column):
sheet.cell(row=1, column=col).font = headerFontObj
wb.save('travelBlogParsed.xlsx')
Thanks in advance,
Johnny
I figured it out. The issue was the following lines:
sheet.column_dimensions['H','I'] = 20
sheet.column_dimensions['B','F','J','K','L'] = 40
sheet.column_dimensions['D','E'] = 50
First of all, apparently, you can't use the column_dimensions method on a vector. The argument must be a string. Second, these lines were missing the .width attribute. So those lines should have been:
sheet.column_dimensions['H'].width = 20
sheet.column_dimensions['I'].width = 20
sheet.column_dimensions['B'].width = 40
...
sheet.column_dimensions['E'].width = 50
I am trying to set the pageorientation in an OpenOffice document with python. The following code i use to try this:
import json
from win32com.client import Dispatch as Dispatch
svm = Dispatch("com.sun.star.ServiceManager")
svm._FlagAsMethod("Bridge_GetStruct")
coreflect = svm.createInstance("com.sun.star.reflection.CoreReflection")
desktop = svm.createInstance("com.sun.star.frame.Desktop")
doc = desktop.loadComponentFromURL("private:factory/swriter", "_blank",0, [])
txt = doc.getText()
cur = txt.createTextCursor()
Then i tried two different approaches:
p = doc.getPagePrintSettings()
p[8].Value = True
doc.setPagePrintSettings(p)
and
oStyleFamilies = doc.getStyleFamilies()
oObj1 = oStyleFamilies.getByName("PageStyles")
oObj2 = oObj1.getByName("Default")
oObj2.IsLandscape = True
Both give no error, but the page is still in Portrait.. Anybody has an idea?
Thanks in advance!!
try : oObj2.setPropertyValue("IsLandscape",True)
see http://codesnippets.services.openoffice.org/Calc/Calc.SwitchOrientation.snip