Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_text_xml(self):
# Test case for text xml object
text = 'New Title'
font_size, text_color, pix_to_inch = 12, '#ff0000', 10000
target = pptgen.pptgen(
source=self.input, only=1,
change={
'Title 1': {
'style': {
'color': text_color,
'font-size': font_size
},
'text': text
}
})
eq_(target.slides[0].shapes.title.text, 'New Title Green Bold Text')
text_shape = self.get_shape(target, 'Title 1')[0]
total_runs = 2
# Maximum one paragraph should be there, because entire text area is being changed
eq_(len(text_shape.text_frame.paragraphs), 1)
# Maximum two runs should be there, because we are adding two seperate style for texts
input_rect = Presentation(self.input)
for update_data in change_data:
# Getting required param from config to compare with output
orient = update_data['orient']
shpname = update_data['shapename']
slidenumber = update_data['slidenumber']
# Getting shape name
shapes = input_rect.slides[slidenumber - 1].shapes
_shp = [shape for shape in shapes if shape.name == shpname][0]
# width, height = _shp.width, _shp.height
height = _shp.height if orient == 'horizontal' else _shp.width
width = _shp.width if orient == 'horizontal' else _shp.height
lo = update_data.get('lo', 0)
hi = update_data.get('hi', self.data['sales'].max())
good = 100
target = pptgen.pptgen(
source=self.input, only=slidenumber,
data={'data': self.data},
draw_bullet={
shpname: {
'bullet': {
'data': 'data["data"]["sales"].loc[0]',
'max-width': 1,
'poor': update_data['poor'],
'good': good,
'lo': lo,
'hi': hi,
'target': 'data["data"]["sales"].max()',
'average': 'data["data"]["sales"].mean()',
'orient': orient,
'gradient': 'Oranges',
'text': update_data['text']
def draw_chart(self, slidenumber, data, rule):
# Function to change data in pptx native charts.
return pptgen.pptgen(
source=self.input,
only=slidenumber,
data={'data': data},
edit_chart=rule)
def test_source_with_target(self):
# Test case to compare target output title.
pptgen.pptgen(source=self.input, target=self.output, only=1)
target = Presentation(self.output)
eq_(len(target.slides), 1)
eq_(target.slides[0].shapes.title.text, 'Input.pptx')
def test_css(self):
# Test case for `css` command.
pix_to_inch = 10000
spec = {'width': 200, 'height': 200, 'top': 100, 'left': 50}
opacity_val, opacity_constant = 0.1, 100000
target = pptgen.pptgen(
source=self.input, only=8,
change={
'Rectangle 1': {
'css': {
'style': {
'opacity': opacity_val,
'color': '#ff0000',
'fill': '#ffff00',
'stroke': '#00ff00',
'width': spec['width'],
'height': spec['height'],
'left': spec['left'],
'top': spec['top']
}
}
}
def test_table(self):
# Function to test `table` command.
with assert_raises(AttributeError):
pptgen.pptgen(
source=self.input, only=25,
data={'data': {}},
change={
'Invalid Input': {
'table': {
'data': 'data'
}
}
})
target = pptgen.pptgen(
source=self.input, only=9,
data={'data': self.data},
change={
'Table 1': {
'table': {
def test_change_chart(self):
# Test case for all native charts charts.
with assert_raises(AttributeError):
pptgen.pptgen(
source=self.input, only=25,
data={'data': {}},
change={
'Invalid Input': {
'chart': {
'data': 'data'
}
}
})
xaxis, opacity = 'city', 0.5
slidenumbers = AttrDict(
Bar_Chart=2, Column_Chart=10, Line_Chart=11, Area_Chart=12,
Scatter_Chart=13, Bubble_Chart=14, Bubble_Chart_3D=15,
Radar_Chart=16, Donut_Chart=17, Pie_Chart=18)
for chart_name, slidenumber in slidenumbers.items():
def test_data_format(self):
# Testing data section. Data argument must be a `dict` like object
with assert_raises(ValueError):
pptgen.pptgen(source=self.input, only=1, data=[1, 2])
for case in [{}, {'function': '{}'}, {'data': [1, 2, 3]}]:
pptgen.pptgen(source=self.input, only=1, data=case)
def test_treemap(self):
# Test case for treemap.
# If shape is not a rectangle
with assert_raises(NotImplementedError):
pptgen.pptgen(
source=self.input, only=25,
data={'data': {}},
drawtreemap={'Invalid Input': {'treemap': {'data': 'data'}}})
target = pptgen.pptgen(
source=self.input, only=19,
data={'data': self.data},
drawtreemap={
'Treemap Rectangle': {
'treemap': {
'data': 'data["data"]',
'keys': ['city'],
'values': '{"sales": "sum", "growth": "sum"}',
'size': {'function': 'lambda v: v["sales"]'},
'sort': {
'function': 'lambda v: v.sort_values(by=["sales"], ascending=False)'
def get(self):
# pptgen is not required unless PPTXHandler is used
from gramex.pptgen import pptgen # noqa
target = io.BytesIO()
pptgen(target=target, handler=self, **self.kwargs)
# Set up headers
headers = self.kwargs.get('headers', {})
headers.setdefault('Content-Type', _mime)
for key, val in headers.items():
self.set_header(key, val)
self.write(target.getvalue())