I have written a python program to take images picked by the user and insert them into a word document. For each image, the program with create a 1x2 table. The top cell will be the image and the bottom cell will be the image name.
My issue is that when running the program, the created tables don't autofit the images. It leaves the image at full scale cutting off most of the image. (see image below)
What my Program does
If you just run word on it's own and create a 1x2 table and insert an image it will automatically set the scale of the image to fit the whole image. This is what I would like my program to do. (see image below)
What I want my program to do
I am using the docx python library. See below for Libraries used
Libraries
I found this article:
https://python-docx.readthedocs.io/en/latest/dev/analysis/features/table/table-props.html
and tried "table.allow_autofit = True" but this did not work. See code below
Create Table
I can manually set the image size as seen commented out in my code, but I would like to not have to do this.
EDIT-
Below is my entire function that I need help with. Please let me know if you need anymore. I didn't post all the code because it's a little long. I am using tkinter to ask the user to enter a title and select the files they would like to import.
def auto_gen():
doc = docx.Document()
section = doc.sections[0]
header = section.header
header_para = header.paragraphs[0]
header_para.style = doc.styles.add_style('Style Name', WD_STYLE_TYPE.PARAGRAPH)
font = header_para.style.font
font.size = Pt(20)
font.bold = True
header_para.paragraph_format.alignment = WD_ALIGN_PARAGRAPH.CENTER
header_para.text = eid.get()
f = filedialog.askopenfilenames()
for x in f:
caption = os.path.basename(x)
f_name, f_ext = os.path.splitext(caption)
table = doc.add_table(rows=2, cols=1)
table.style = 'Table Grid'
run1 = table.cell(0,0).paragraphs[0].add_run()
run2p = table.cell(1,0).paragraphs[0]
run2 = table.cell(1,0).paragraphs[0].add_run()
run1.add_picture(x)#, height = Inches(3.38), width = Inches(6))
run2.text = (f_name)
run2p.paragraph_format.alignment = WD_ALIGN_PARAGRAPH.CENTER
doc.add_paragraph()
output = eid.get() + '.docx'
doc.save(output)
I've been using python and reportlab to auto-generate long documents and want to use the PDF outline tree for easy navigation through the document. According to the docs, canvas.bookmarkPage comes with multiple options to adjust the document view after jumping to the destination page. The standard one is a simple page Fit to the window. From a user perspective, I would prefer FitH (as wide as possible, with destination at the top of the screen) or XYZ (keep user zoom level with destination at the top of the screen). When using any fit option except the basic Fit, the function call must be provided with the coordinates to arrange the view accordingly.
However, I could not find any explanations, examples, code snippets, or anything on how to figure this out, and it took me a good while to come up with a solution. So, I want to share this solution here and ask if this is really the best way to do it or if I overlooked something basic.
The key thing here is SmartParagraph which remembers its position after it was drawn. First, I used flowable.canv.absolutePosition(0,0) in the afterFlowable() method because this is where I needed this information to pass it to bookmarkPage(). However, the position was always reported as 0, 0, so apparently the flowable and/or the canvas have forgotten everything about the position when afterFlowable() is reached. So I thought there has to be some point in time when a Flowable knows its position and after investigating the source code I found out that after draw(), it still knows where it is.
So: SmartParagraph is a subclass of Paragraph that stores its position after it is drawn, so that later in the document building process this can be used by any external element for whatever.
The example will create a dummy pdf with 2 headings that do a nice FitH zoom and two headings that do the basic Fit zoom.
Does anyone have a better idea on how to solve this?
import typing
from reportlab.lib.styles import ParagraphStyle as PS
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.platypus.flowables import Flowable
from reportlab.platypus import PageBreak, Spacer
from reportlab.platypus.paragraph import Paragraph
from reportlab.platypus.doctemplate import SimpleDocTemplate
from reportlab.lib.units import cm
class SmartParagraph(Paragraph):
def __init__(self, text, *args, **kwds):
"""This paragraph remembers its position on the canvas"""
super(SmartParagraph, self).__init__(text, *args, **kwds)
self._pos: typing.Tuple[int, int] = None
def draw(self):
super(SmartParagraph, self).draw()
self._pos = self.canv.absolutePosition(0, 0)
def get_pos(self) -> typing.Tuple[int, int]:
return self._pos
class CustomDocTemplate(SimpleDocTemplate):
def __init__(self, filename, outline_levels: int = 4, **kwargs):
super(CustomDocTemplate, self).__init__(filename, **kwargs)
self._bookmark_keys = list()
if not isinstance(outline_levels, int) and outline_levels < 1:
raise ValueError("Outline levels must be integer and at least 1")
self._outline_levels = {f'Heading{level+1}': level for level in range(outline_levels)}
# Map of kind: Heading1 -> 0
# Heading 1 is level 0, I dont make the rules
def afterFlowable(self, flowable: Flowable):
"""Registers TOC entries."""
if isinstance(flowable, Paragraph):
flowable: Paragraph
text = flowable.getPlainText()
style = flowable.style.name
if style in self._outline_levels:
level = self._outline_levels[style]
else:
return
if text not in self._bookmark_keys:
key = text
self._bookmark_keys.append(key)
else:
# There might headings with identical text, yet they need a different key
# Keys are stored in a list and incremented if a duplicate is found
cnt = 1
while True:
key = text + str(cnt)
if key not in self._bookmark_keys:
self._bookmark_keys.append(key)
break
cnt += 1
if isinstance(flowable, SmartParagraph):
# Only smart paragraphs know their own position
x, y = flowable.get_pos()
y += flowable.style.fontSize + 15
self.canv.bookmarkPage(key, fit="FitH", top=y)
else:
# Dumb paragraphs need to show the whole page
self.canv.bookmarkPage(key)
self.canv.addOutlineEntry(title=text, key=key, level=level)
def _endBuild(self):
"""Override of parent function. Shows outline tree by default when opening PDF."""
super(CustomDocTemplate, self)._endBuild()
self.canv.showOutline()
story = list()
story.append(SmartParagraph('First Smart Heading', getSampleStyleSheet()['h1']))
story.append(Paragraph('Text in first heading'))
story.append(Spacer(1, 0.5 * cm))
story.append(SmartParagraph('First Sub Smart Heading', getSampleStyleSheet()['h2']))
story.append(Paragraph('Text in first sub heading'))
story.append(Spacer(1, 0.5 * cm))
story.append(Paragraph('Second Sub Dumb Heading', getSampleStyleSheet()['h2']))
story.append(Paragraph('Text in second sub heading'))
story.append(PageBreak())
story.append(Paragraph('Last Dumb Heading', getSampleStyleSheet()['h1']))
story.append(Paragraph('Text in last heading', PS('body')))
doc = CustomDocTemplate('mintoc.pdf')
doc.multiBuild(story)
I'm trying to create and place a series of sketches from a tablet to a blender file. I know where they should go in 3d-space. I am trying to construct a python script to input a file name and coordinates then place thing at location.
From some code I found.. I think this is very close. The script leaves off at allowing the user to draw. Instead I want to import a file.
import bpy
context = bpy.context
space = context.space_data
# Create material for grease pencil
if "Bright Material" in bpy.data.materials.keys():
gp_mat = bpy.data.materials["Bright Material"]
else:
gp_mat = bpy.data.materials.new("Bright Material")
if not gp_mat.is_grease_pencil:
bpy.data.materials.create_gpencil_data(gp_mat)
gp_mat.grease_pencil.color = (1, 0, 0.818649, 1)
# Add grease pencil object
gp_data = bpy.data.grease_pencils.new("Bright Pencil")
gp_ob = bpy.data.objects.new("Bright Pencil", gp_data)
context.scene.collection.objects.link(gp_ob)
if space.local_view:
gp_ob.local_view_set(space, True)
for ob in context.selected_objects:
ob.select_set(False)
gp_ob.select_set(True)
context.view_layer.objects.active = gp_ob
bpy.ops.object.mode_set(mode='PAINT_GPENCIL')
# Assign the material to the grease pencil for drawing
gp_data.materials.append(gp_mat)
next instead of bpy.ops.gpencil.draw(wait_for_input=False)
can I do something like bpy.context.image_paint_object
or bpy.ops.object.load_reference_image
or otherwise import an SVG image and set the rotation of it? can I add a frame number for animation?
I have script which adds a textbox with some text on an existing PPT. Now the textbox colour is made white to overwrite the existing text present in the slidemaster.
The issue is than a small part of textbox overlaps with another shape which is supposed to be on top. Is there an option in python-pptx to send the shape to back.
Below is the option which can be used using the powerpoint
Is the a way I can do this using python-pptx
here is my script
for pptfile in addressList:
prs = Presentation(pptfile)
slides = prs.slides
for i in range(2,len(slides)-1):
textContent = ""
slide = prs.slides[i]
# Text position
t_left = Inches(3.27)
t_top = Inches(7.05)
t_width = Inches(6.89)
t_height = Inches(0.27)
# Text
txBox = slide.shapes.add_textbox(t_left, t_top, t_width, t_height)
fill = txBox.fill
fill.solid()
fill.fore_color.rgb = RGBColor(255, 255, 255)
tf = txBox.text_frame.paragraphs[0]
tf.vertical_anchor = MSO_ANCHOR.TOP
tf.word_wrap = True
tf.margin_top = 0
tf.auto_size = MSO_AUTO_SIZE.SHAPE_TO_FIT_TEXT
run = tf.add_run()
run.text = "This is new text."
font = run.font
font.name = 'Univers LT Std 47 Cn Lt'
font.size = Pt(10)
font.bold = None
font.italic = None # cause value to be inherited from theme
font.color.rgb = RGBColor(157, 163, 163)
prs.save(pptfile)
print(pptfile," Done!")
This discussion in github might help you:
The z-order of shapes on a slide is determined solely by their document order in the slide part (e.g. slide1.xml). So the general gist would be to re-order that sequence of elements. The shapes in a slide are contained in the slide's "shape tree", a element with the same syntax as the group shape, just the different name. The object I expect you'll want to look at first is pptx.shapes.shapetree.SlideShapeTree and its parent BaseShapeTree, which is what you get from slide.shapes. The _spTree attribute on that object gives you the lxml object for the element, which would allow you to reorder shapes.
[...]
I believe the .addprevious() and .addnext() lxml methods actually move the XML element in question.
So you could do something like this to move a shape from ninth position to fourth:
# shape will be positioned relative to this one, hence the name "cursor"
cursor_sp = shapes[3]._element
cursor_sp.addprevious(shapes[8]._element)
See github question
I have been developing a GUI for reading continuous data from a serial port. After reading the data, some calculations are made and the results will be plotted and refreshed (aka dynamic plotting). I use the wx backend provided in the matplotlib for this purposes. To do this, I basically use an array to store my results, in which I keep appending it to, after each calculation, and replot the whole graph. To make it "dynamic", I just set the x-axis lower and upper limits for each iteration. Something like found in:
http://eli.thegreenplace.net/2008/08/01/matplotlib-with-wxpython-guis/
The problem, however, is that since the data is continuous, and if I keep plotting it, eventually the system memory will run out and system will crash. Is there any other way I can plot my result continuously?
To do this, I basically use an array
to store my results, in which I keep
appending it to
Try limiting the size of this array, either by deleting old data or by deleting every n-th entry (the screen resolution will prevent all entries to be displayed anyway). I assume you write all the data to disk so you won't lose anything.
Also, analise your code for memory leaks. Stuff you use and don't need anymore but that doesn't get garbage-collected because you still have a reference to it.
I have created such a component with pythons Tkinter. The source is here.
Basically, you have to keep the plotted data somewhere. You cannot keep an infinite amount of data points in memory, so you either have to save it to disk or you have to overwrite old data points.
Data and representation of data are two different things. You might want to store your data to disk if it's important data to be analyzed later, but only keep a fixed period of time or the last N points for display purposes. You could even let the user pick the time frame to be displayed.
I actually ran into this problem (more of a mental block, actually...).
First of all I copy-pasted some wx Plot code from wx Demo Code.
What I do is keep a live log of a value, and compare it to two markers (min and max, shown as red and green dotted lines) (but I will make these 2 markers optional - hence the optional parameters).
In order to implement the live log, I first wanted to use the deque class, but since the data is in tuple mode (x,y coordinates) I gave up and just tried to rewrite the entire parameter list of tuples: see _update_coordinates.
It works just fine for keeping track of the last 100-10,000 plots. Would have also included a printscreen, but I'm too much of a noob at stackoverflow to be allowed :))
My live parameter is updated every 0.25 seconds over a 115kbps UART.
The trick is at the end, in the custom refresh method!
Here is most of the code:
class DefaultPlotFrame(wx.Frame):
def __init__(self, ymin=0, ymax=MAXIMUM_PLOTS, minThreshold=None,
maxThreshold=None, plotColour='blue',
title="Default Plot Frame",
position=(10,10),
backgroundColour="yellow", frameSize=(400,300)):
self.minThreshold = minThreshold
self.maxThreshold = maxThreshold
self.frame1 = wx.Frame(None, title="wx.lib.plot", id=-1, size=(410, 340), pos=position)
self.panel1 = wx.Panel(self.frame1)
self.panel1.SetBackgroundColour(backgroundColour)
self.ymin = ymin
self.ymax = ymax
self.title = title
self.plotColour = plotColour
self.lines = [None, None, None]
# mild difference between wxPython26 and wxPython28
if wx.VERSION[1] < 7:
self.plotter = plot.PlotCanvas(self.panel1, size=frameSize)
else:
self.plotter = plot.PlotCanvas(self.panel1)
self.plotter.SetInitialSize(size=frameSize)
# enable the zoom feature (drag a box around area of interest)
self.plotter.SetEnableZoom(False)
# list of (x,y) data point tuples
self.coordinates = []
for x_item in range(MAXIMUM_PLOTS):
self.coordinates.append((x_item, (ymin+ymax)/2))
self.queue = deque(self.coordinates)
if self.maxThreshold!=None:
self._update_max_threshold()
#endif
if self.lockThreshold!=None:
self._update_min_threshold()
#endif
self.line = plot.PolyLine(self.coordinates, colour=plotColour, width=1)
self.lines[0] = (self.line)
self.gc = plot.PlotGraphics(self.lines, title, 'Time', 'Value')
self.plotter.Draw(self.gc, xAxis=(0, MAXIMUM_PLOTS), yAxis=(ymin, ymax))
self.frame1.Show(True)
def _update_max_threshold(self):
if self.maxThreshold!=None:
self.maxCoordinates = []
for x_item in range(MAXIMUM_PLOTS):
self.maxCoordinates.append((x_item, self.maxThreshold))
#endfor
self.maxLine = plot.PolyLine(self.maxCoordinates, colour="green", width=1)
self.maxMarker = plot.PolyMarker(self.maxCoordinates, colour="green", marker='dot')
self.lines[1] = self.maxMarker
#endif
def _update_live_param(self, liveParam, minParam, maxParam):
if minParam!=None:
self.minThreshold = int(minParam)
self._update_min_threshold()
#endif
if maxParam!=None:
self.maxThreshold = int(maxParam)
self._update_max_threshold()
#endif
if liveParam!=None:
self._update_coordinates(int(liveParam))
#endif
def _update_coordinates(self, newValue):
newList = []
for x,y in self.coordinates[1:]:
newList.append((x-1, y))
#endfor
newList.append((x, newValue))
print "New list", newList
self.line = (plot.PolyLine(newList, colour=self.plotColour, width=1))
self.lines[0] = self.line
self.coordinates = newList
def _MyLIVE_MAGIC_refresh__(self, liveParam=None, minParam=None, maxParam=None):
self._update_live_param(liveParam, minParam, maxParam)
self.gc = plot.PlotGraphics(self.lines, self.title, 'Time', 'Value')
self.plotter.Draw(self.gc, xAxis=(0, MAXIMUM_PLOTS), yAxis=(self.ymin, self.ymax))
self.plotter.Refresh()
self.frame1.Refresh()