Importing slides from an existing PPT and add the slides to a new PPT using officer for R - powerpoint

Can you please provide an example of using Officer to import slides from an existing PPT, and then copying one or more of the slides into a new PPT? I did not see this at https://ardata-fr.github.io/officeverse/
I have found a backwards work-around:
my_pres <- read_pptx()
my_pres <- read_pptx(path = "one_slide.pptx") # The pptx can have any number of slides
my_pres <- add_slide(my_pres)
my_pres <- ph_with(my_pres, "Hello world", location = ph_location_type(type = "title"))
move_slide(my_pres, 1,length(my_pres))
print(my_pres, target = 'one_slide_appended.pptx')
but this limits to including slides from a single source
thanks much

Related

Need to read in 16 bit unit gray scale not png or jpg data

In Tensorflow decode_png and decode_jpg are used to decode data read in from a source (file, url,…).
I have medical type images in a 8192x8192 gray scale 16 bit uint format and don’t need png or jpg decoding. These medical images are external files.
Over the last week I’ve tried numerous approaches, sifted through stack overflow, Github and Tensorflow web pages but have not been able to resolve the issue of not being able to modify the code to read in non png or jpg data. I’m modifying the pix2pix.py code:
input_paths = glob.glob(os.path.join(a.input_dir, "*.jpg"))
decode = tf.image.decode_jpeg
if len(input_paths) == 0:
input_paths = glob.glob(os.path.join(a.input_dir, "*.png"))
decode = tf.image.decode_png
path_queue = tf.train.string_input_producer(input_paths, shuffle=a.mode == "train")
reader = tf.WholeFileReader()
paths, contents = reader.read(path_queue)
raw_input = decode(contents) #
raw_input = tf.image.convert_image_dtype(raw_input, dtype=tf.float32)
I’ve tried numerous approaches to get through/by/around: “raw_input = decode(contents)“
Thanks in advance for any help/pointers.

poster image not displaying with add_movie() in python-pptx

I am trying to insert some sound files into a presentation, and the sound file seems to save fine, but the display image is always the default play button logo. Is there something wrong with my code, or is it another issue. I am currently working in a linux environment, if that makes any difference. I have tried with both mp4 and mp3 and the image issue is the same. The small play bar also seems not to appear although the sound file is in the presentation.
from pptx import Presentation
from pptx.util import Inches
prs = Presentation()
title_slide_layout = prs.slide_layouts[0]
slide = prs.slides.add_slide(title_slide_layout)
prs.slides[0].shapes.add_movie("sample2.mp3",
left = Inches(1), top = Inches(1), width = Inches(1), height = Inches(1),
poster_frame_image = "cat.jpeg"
)
prs.save('sound_image.pptx')
You can easily add any sound files to presentations and set poster images for them by using Aspose.Slides for Python as shown below:
import aspose.slides as slides
presentation_path = "example.pptx"
audio_path = "sample2.mp3"
image_path = "cat.jpeg"
with slides.Presentation() as presentation:
slide = presentation.slides[0]
# Add an audio frame to the slide with a specified position and size.
with open(audio_path, 'rb') as audio_stream:
audio_frame = slide.shapes.add_audio_frame_embedded(150, 100, 50, 50, audio_stream)
# Add the image to presentation resources.
with open(image_path, 'rb') as image_stream:
audio_image = presentation.images.add_image(image_stream)
# Set the image as the audio poster.
audio_frame.picture_format.picture.image = audio_image
presentation.save(presentation_path, slides.export.SaveFormat.PPTX)
I work as a Support Developer at Aspose.

Copy slide with images python pptx

My end goal is to change the theme of a presentation. To do this, I have created a source template and new template (with the correct theme). I iterate over each slide in the source template then add a new slide to the new template with the contents of the source using the code below - source. If there is a better way to do this I'd love to hear it.
This works great for text and text boxes, however the test image cannot be displayed in the new powerpoint (show in the image below):
Code
def copy_slide_from_external_prs(self, src, idx, newPrs):
# specify the slide you want to copy the contents from
src_slide = src.slides[idx]
# Define the layout you want to use from your generated pptx
slide_layout = newPrs.slide_layouts[2]
# create now slide, to copy contents to
curr_slide = newPrs.slides.add_slide(slide_layout)
# remove placeholders
for p in [s.element for s in curr_slide.shapes if 'Text Placeholder' in s.name or 'Title' in s.name]:
p.getparent().remove(p)
# now copy contents from external slide, but do not copy slide properties
# e.g. slide layouts, etc., because these would produce errors, as diplicate
# entries might be generated
for shp in src_slide.shapes:
el = shp.element
newel = copy.deepcopy(el)
curr_slide.shapes._spTree.insert_element_before(newel, 'p:extLst')
return newPrs
I was trying many different solutions and tried creating a new Picture using the image.blob property in the source image. However, then the image does not have an element. Do I need to convert the blob to a PNG, save it, then create a new image using that saved PNG?
There must be a better way to do this. Again, I just want to change the theme.
Thanks in advance!
Here is the workaround I developed. I first check if the shape is an image, and if it is, I write the image to a local directory. Then I add a picture to the slide using that saved image. Finally, I delete the locally saved image.
Now this copy_slide function works for images:
def copy_slide_from_external_prs(src, idx, newPrs):
# specify the slide you want to copy the contents from
src_slide = src.slides[idx]
# Define the layout you want to use from your generated pptx
SLD_LAYOUT = 5
slide_layout = prs.slide_layouts[SLD_LAYOUT]
# create now slide, to copy contents to
curr_slide = newPrs.slides.add_slide(slide_layout)
# create images dict
imgDict = {}
# now copy contents from external slide, but do not copy slide properties
# e.g. slide layouts, etc., because these would produce errors, as diplicate
# entries might be generated
for shp in src_slide.shapes:
if 'Picture' in shp.name:
# save image
with open(shp.name+'.jpg', 'wb') as f:
f.write(shp.image.blob)
# add image to dict
imgDict[shp.name+'.jpg'] = [shp.left, shp.top, shp.width, shp.height]
else:
# create copy of elem
el = shp.element
newel = copy.deepcopy(el)
# add elem to shape tree
curr_slide.shapes._spTree.insert_element_before(newel, 'p:extLst')
# add pictures
for k, v in imgDict.items():
curr_slide.shapes.add_picture(k, v[0], v[1], v[2], v[3])
os.remove(k)
Using code from #Michael-Berk I made very elegant method for copying slides (including images). Check this answer: https://stackoverflow.com/a/73954830/20159015

With NPOI, set Word paragraph to Heading 1 style

When using NPOI to create a Word document, how does one set a paragraph to the built-in "Heading 1" style?
Here is what I have tried in F#:
let doc = XWPFDocument()
let p = doc.CreateParagraph()
p.Style <- "Heading 1"
let r = p.CreateRun()
r.SetText("Hello, world")
When I open the generated file in Word, the line "Hello, world" is not in the Heading 1 style.
Here is what I did:
Using Word,
Create a new Blank document.
Include a paragraph that has the style(s) of interest.
Save the document to your project folder.
In code using NPOI,
Load the blank document.
Delete the contents of the document.
Write to your document, setting the paragraph style with the styleId.
Here is an example:
use fsSrc = new FileStream("blank.docx", FileMode.Open, FileAccess.Read)
let doc = XWPFDocument(fsSrc)
while doc.RemoveBodyElement(0) do ()
let p = doc.CreateParagraph()
p.Style <- "Heading1"
let r = p.CreateRun()
r.SetText("Hello, world")
Today I learned...
The default styles are not included when creating a new XWPFDocument().
The styles are identified by their styleId, not their user friendly names. The styleId for "Heading 1" is Heading1.

EPPlus Copying multiple sheets into a single sheet with images

I am trying to copy a workbook with multiple sheets into a single sheet. I have found other libraries that work fine for copying ranges or sheets into the bottom of another but they don't copy the images. Is this possible with EPPlus? If it isn't are there other libraries that do this?
I was able to get this accomplished by using GemBox.Spreadsheet. The were quite responsive to bug(s) in their library and it all functions correctly now.
var excelDrawings = sheet1.Drawings.Where(x => x.DrawingType ==
eDrawingType.Picture).ToList();
foreach (var excelDrawing in excelDrawings)
{
var name = excelDrawing.Name;
var image = excelDrawing.As.Picture.Image;
singleSheet.Drawings.AddPicture(name, image);
}

Resources