With md2pptx the user generally supplies colours as RGB values. I'd like an alternative where they can use eg ACCENT_1 or whatever the template presentation calls it. Motivation: You probably don't know the RGB value of a colour in a template you're handed.
Is there a programmatic method - either in python-pptx - or spelunking in the XML to retrieve the colours' RGB values, along with their names? I don't mean "pale violet"; I do mean eg "ACCENT_1".
If not I suppose I could be confecting XML that uses these names. Might that be a better idea anyway?
Anywhere you can specify an RGB color you can also specify a so-called "theme" or sometimes called a "scheme" color.
Like:
from docx.enum.dml import MSO_THEME_COLOR
font.color.theme_color = MSO_THEME_COLOR.ACCENT_2
Related
I am not using RGB but rather HEX values. The data base looks something like this:
Manufacturer, Color Name, Hex value, Red value, Green value, Blue value
From reading up on this Euclidian is the best way to go. I have found code that uses RGB that does something similar, but I am looking to use Hex. I will need to convert Hex to RGB I assume. Then use the shortest distance. My struggle is how do I use the HEX and how do I return the manufacturer and Color name from the database using python.There has to be existing code for this, if someone would be kind enough to share. Yes I am new to this.
I want to read in a given datalabel's text.
What I have tried:
print(plot.series[0].points[0].data_label.text_frame.text)
Snippet above tries to print the 1st series' first point which is '16' but it prints nothing.
How can I obtain what is in the datalabel?
I want to read the text in, concat something new to it and reinsert it into the data label. Something like this
dltext = plot.series[0].points[0].data_label.text_frame.text
plot.series[0].points[0].data_label.text_frame.text = dltext + "Foo"
The data_label.text_frame only contains text if you put it there explicitly. Otherwise what is rendered is a function of the value of that data-point and the settings .show_value and show_percent, etc. documented here: https://python-pptx.readthedocs.io/en/latest/api/chart.html#pptx.chart.datalabel.DataLabels
If you want to match what shows to the user you'll need to duplicate that logic.
If you wanted to accomplish that for the general case, it would take some doing because you'd need to compute the effective value of properties like DataLabel.show_value, which would require reverse-engineering the style hierarchy for that setting.
But the 95% solution would just be to assume what is showing is the value and go with that. That's the default data label, at least for bar charts (pie charts may default to percent).
I am using OpenCV to detect BRISK keypoints this way:
img = cv2.imread(image_path)
gray= cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
kp, descriptors = cv2.BRISK_create().detectAndCompute(gray,None)
kp is a list, however, it is also a kind of struct. It has parameters or "sublists" such as kp.pt values, kp. response values and so on. I am a little rookie in Python so I would like to know the following.
1-Is there a specific name for this list? it does not seem to be the list I am used to using in Python. How to get more information about this kind of list?
2- How can I know what are the other parameters (or "sublists") of kp? can I print them in Python?
3- How can I save in a variable all elements of, for example, kp.response? I can only index individual elements like this kp[1].response
4-How can I sort kp.response values, getting the indices of the N highest kp.response values?
1- type(kp) This gets the class that the object is an instance of.
2- dir(kp) This should list the attributes of the object. For certain very dynamic types that generate their attributes on the fly in response to access attempts, this might not work. (But it still might, since dir can also be generated dynamically.)
In that case, if you can find the __getattr__ method in the source code of the class, it might give you a clue where those dynamic attributes are actually stored.
3- [e.response for e in kp] probably. Usually if it's indexible like that it's also iterable.
4- print([i for _, i in sorted((e.response, i) for i, e in enumerate(kp))[-N:]]) haven't tested this.
Also try help(kp), it might explain how to use it better. Use help on anything you don't understand. It might have a docstring.
Now I just need to figure out how to use the resulting list as kp indexes
Maybe something like [kp[i] for i in indexes]?
How can I invert (rotate 180 degrees) a text object so that the text is kerned appropriately?
My example uses Python and the svgwrite package, but my question seems about any SVG.
Suppose I use the following code:
dwg = svgwrite.Drawing()
dwg.add(dwg.text(fullName, (int(width/2.),gnameHeight),
font_size=gnameFontSize, text_anchor="middle"))
The above code generates text looking like this:
dwg.text() objects accept a rotate parameter that is applied to all characters in a text string, so I've used the following code to reverse the string first:
pcRotate = [180]
ngap = 1
revFullName = fullName
rcl = []
for c in revFullName:
rcl.append(c)
for i in range(ngap):
rcl.append(' ')
rcl.reverse()
revFullName = ''.join(rcl)
dwg.add(dwg.text(revFullName, (int(width/2.),pcnameHeight),
font_size=gnameFontSize, text_anchor="middle", rotate=pcRotate))
But, this produces the very ugly version below:
and this is using an artificial space gap between characters to make it slightly less unreadable.
What's the best way to tap into whatever kerning is being used by standard text in this inverted situation?
The rotate attribute of a <text> element is intended for situations where you want to rotate individual characters. If you want to rotate the whole text object then you should be using a transform instead.
http://pythonhosted.org/svgwrite/classes/mixins.html#transform-mixin
I'm posting this as a self-answer, only to make formatting more clear. Two useful hints from #paul-lebeau happily acknowledged.
While the svgwrite package seems solid, its documentation is a bit thin. The two things I wish it had said:
The rotate attribute of a <text> element is intended for situations where you want to rotate individual characters. If you want to rotate the whole text object, then you should be using a transform mixin instead.
If you need to center the transformed text with respect to some center (other that the default current user coordinate system), add two additional parameters xctr,yctr. This differs from the doc which calls for a single center argument that is a (2-tuple).
The correct code is:
pcRotate = 'rotate(180,%s,%s)' % (int(width/2.),pcnameHeight)
textGroup = svgwrite.container.Group(transform=pcRotate)
textGroup.add(dwg.text(fullName, (int(width/2.),pcnameHeight),
font_size=gnameFontSize, text_anchor="middle"))
dwg.add(textGroup)
I borrowed this python code here (first answer by enno groper) to automate the extraction of annotations from pdfs.
I want to make some modifications to the code. Trying to fetch the color of annotations with annot_mapping.annot.get_color() I ran into the first issue. What the command returns is objects like this one <PopplerColor at 0x1a85180>, rather than rgb values (promised here).
According to poppler docs poppler_annot_get_color() returns "a new allocated PopplerColor with the color values of poppler_annot , or NULL. It must be freed with g_free() when done".
Is this correct and if yes, how do I achieve this in python?
annot_mapping.annot.get_color() gives you a PopplerColor, which is a structure with three members (of type guint16): red, green, and blue. For example:
PopplerColor *color = poppler_annot_get_color (annot);
g_printf ("%d\n", color->red);
gives you the red value of your annotation annot, the gb values can be obtained similarly.
In python this is achieved through annot_mapping.annot.get_color().red, assuming you have import poppler