Picture can't be displayed when adding picture to copied table using python-docx - python-docx

I'm using python-docx to edit an existing word file. I've created a simpler case where it also fails starting with a blank document. Adding a picture to a cell in a table works just as expected. But if I want to copy that table, there seems to be a problem when I add an image to the new table. I've looked elsewhere, but this problem seems to be unique to copied tables only.
from copy import deepcopy
from docx import Document
doc = Document()
table1 = doc.add_table(rows=2, cols=2)
table1.style = 'TableGrid'
table2 = deepcopy(table)
table2._tbl = deepcopy(table1._tbl)
p = doc.add_paragraph()
p._p.addnext(table2._tbl)
# Add image to table 1
cell = table1.rows[0].cells[0]
cell.paragraphs[0].add_run().add_picture("picture.png", width=Cm(10))
# Add image to table 2
cell = table2.rows[0].cells[0]
cell.paragraphs[0].add_run().add_picture("picture.png", width=Cm(10))
doc.save("t.docx")
Problem: If I add the same image to both, it will appear correct. If I add it to only table 1, it also works. But if I add it to the copy, then it fails and looks like this:
It seems to be something that is different in the second, copied table. I know it looks like a hack the way it was created. I did it this way because, in my original document, the table is quite complex and needs to be duplicated for each case.
(Edit: added missing deepcopyimport)

Related

Adding html attribute to existing table in existing djangitables2 table

I want to add a column with a hyperlink to my existing table which I created using djangotables2
I tried this but did not work for me.
I am able to add an extra column but the header name but the columns do not show anything in the cells. Could you please guide me on it?
Below is my code
class PDFColumn(tables.Column):
def render(self, value):
return format_html('PDF pdf link', value)
class MYTable(tables.Table): #this is my existing lass table on view page
PDF = PDFColumn()
Now able to see any data within the columns, only column appears with header name PDF

Is there a way to access the first element in a column on a website using VBA?

Here is a screenshot of a column in a website page.
It is located in that way in the website page :
As you can see, all the rows have a 'Completed' button you can pres and followed by a number of lines. These rows refer to exports. So the columnis not static and is constantly changing.
However, everytime i run the macro i want to access the first row of the column.
Here is a sample code of he HTML code of the first 'Completed' button in the screenshot above:
I have many that have the same class name. Look at the highlighted rows as an example in the picture below:
I really have no idea how to write a VBA code to always access the first 'Completed' bytton in this column.
PS: In the HTML code, in the tag "a", the onclick="....." is constantly changing. So i cannot use this as an argument to access the desired field and click on the desired button.
Please if anyone could help me figure out how to do this, i would really be happy.
Thank you :)
If you want to click the 'Completed' button in the first column, you can use the code below:
Set doc = objIE.Document
doc.getElementsByTagName("tr")(0).getElementsByTagName("td")(0).getElementsByTagName("a")(0).Click
The code get the first <tr> then get the first <td> then get <a> in it.
<tr> tags are rows, <td> tags are cells inside those rows. You did not provide enough code to show the entire table, but generally speaking to access the first row of a table, you would need to refer to the collection object and use the index number you want.
.getElementsByTagName("tr")(0)
This will refer to the first row of a table. Same with getting the first column in the first row of your table:
.getElementsByTagName("tr")(0).getElementsByTagName("td")(0)
Once you tracked down the particular cell, now you are wanting to click the link. You can use the same method as above.
.getElementsByTagName("tr")(0).getElementsByTagName("td")(0).getElementsByTagName("a")(0).Click
And a final note, the first row of a table could be a header, so you may actually want the 2nd row (1) instead.
Thanks for updating with more HTML code. I am going to slightly switch gears and use querySelector() to grab the main table.
doc.querySelector("#divPage > table.advancedSearch_table > tbody"). _
getElementsByTagName("tr")(3).getElementsByTagName("td")(3).Children(0).Click
See if this works for you.

Example to combine headers footers paragraphs of html and Tables

jspdf-autotable examples['header-footer'] example gets me most of what I need for my task.
I am trying to add rich text (constant font some bold and under line words) before and after a table. looking at examples.content did not make it clear.
So a complete PDF might be:
1. some paragraphs of text
2. a table on more than one page
3. some paragraphs of text
4. another table on more than one page
how do I combine all of this in one var doc = new jsPDF(); ?
Example code would be very appreciated.
The key is to use doc.autoTable.previous.finalY to get the final y position where the table ended drawing. You can than dynamically use that to draw text with doc.text(). If you want further guidence, please update your question with more info on what you have tried and what didn't work.

Get tabledata from html, JSOUP

What is the best way to extract data from a table from an url?
In short I need to get the actual data from the these 2 tables at: http://www.oddsportal.com/sure-bets/
In this example the data would be "Paddy power" and "3.50"
See this image:
(Sorry for posting image like this, but I still need reputation, i will edit later)
http://img837.imageshack.us/img837/3219/odds2.png
I have tried with Jsoup, but i dont know if this is the best way?
And I can't seem to navigate correctly down the tables, I have tried things like this:
tables = doc.getElementsByAttributeValueStarting("class", "center");
link = doc.select("div#col-content > title").first();
String text1 = doc.select("div.odd").text();
The tables thing seem to get some data, but doesn't include the text in the table
Sorry, man. The second field you want to retrieve is filled by JavaScript. Jsoup does not execute JavaScript.
To select title of first row you can use:
Document doc = Jsoup.connect("http://www.oddsportal.com/sure-bets/").get();
Elements tables = doc.select("table.table-main").select("tr:eq(2)").select("td:eq(2)");
System.out.println(tables.select("a").attr("title"));
Chain selects used for visualization.

MySQL Replace query

I have one table, and I need to remove a specific text from a specific field. This field contains a full URL of an image, I need to remove the URL and just keep the image filename.
So:
Current date: fieldname: www.example.com/photo.jpg
What I want to do is remove www.example.com/ from all of the entries for this field.
I know how to use the search and replace function, but I don't know how to leave part of the data intact.
This is what I've used but can't modify it to make it work the way I want:
UPDATE table SET oc_upload1 = REPLACE(oc_upload1,'newtext') WHERE oc_upload1 LIKE "oldtext"
Is this possible? If so, how? Thank you!
This should do:
UPDATE table
SET image = REPLACE(image, 'www.example.com/','')
but, it's possible that image contains 'www.example.com/' as part of image file name so to be extra safe and replace only the first occurence of www.example.com
UPDATE table
SET image = SUBSTRING(image, LENGTH('www.example.com/') + 1)
WHERE image LIKE 'www.example.com/%'
But if You really, really just want the file name and not path to the file You can also use:
UPDATE table
SET image = SUBSTRING_INDEX(image,'/',-1)
Note that above statement will change 'www.example.com/images/01/02/daisy.jpg' to 'daisy.jpg', not 'images/01/02/daisy.jpg'. It also wont change rows that does not contain '/' in image.