Skip to content

add feature: Paragraph.add_hyperlink() #74 #162

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from

Conversation

tanyunshi
Copy link

I continued robertdodd's work to implement the p.add_hyperlink() method:

  • to add links to paragraphs
  • to add multiple texts into a link
  • style the link text(run)

Documentations are also available for the dev part.

from docx import Document
document = Document()
p = document.add_paragraph('some text')
link = p.add_hyperlink('google','http://www.google.com')
google
link.add_run('run1')
< docx.text.run.Run object at 0x7fec7cabe908>
link.add_run('formatted').italic = True
3

@scanny
Copy link
Contributor

scanny commented Mar 20, 2015

Hi @tanyunshi, I won't be able to commit this pull-request as is because it lacks tests and the API has not been vetted. But I thank you for it nonetheless and will keep it open for when we get back to hyperlinks. I'm sure it will be useful to folks in the meantime and we'll use it as a reference when we get to the implementation.

If you're interested in pursuing a commit, the best place to start is with the feature analysis. That's where the discussion that resolves the API happens, and that determines the acceptance tests which in turn of course have a strong impact on the implementation. Let me know if you're interested, happy to help you along the way with any questions on how to get into it.

@tanyunshi
Copy link
Author

Hihi @scanny, I am interested in pursuing commits. In fact I have added some feature analysis for hyperlink in dev\analysis\features. Could you please gives me some advices for the next step?

@akobler
Copy link

akobler commented Mar 9, 2016

Is there a plan to finish/merge this feature back to master? I am interested in inserting hyperlinks via python-docx. Btw, thanks you guys for all your work!

@scanny scanny reopened this Mar 9, 2016
@scanny
Copy link
Contributor

scanny commented Mar 9, 2016

Hi all, apologies, I seem to have lost track of this one for roughly a year somehow ⭕

We're definitely interested in pursuing this feature. @tanyunshi I'll take a look at your analysis page this evening. On first glance it looks quite good. I'll provide some feedback.

Are you still interested in working on this? I know it's been some time :)

@tanyunshi
Copy link
Author

Hi @scanny

Glad that you are interested in this feature. ^^

Yes I think the hyperlink is an important feature and I will work on it. Shall we start with the analysis page? :)

@scanny
Copy link
Contributor

scanny commented Mar 11, 2016

Yes, perfect. I'll take a look at it and provide comments, probably tomorrow, and we can go from there :)

@tanyunshi
Copy link
Author

Hello @scanny,
Thank you for the reviewing. :)

I have taken your advise and improved the feature analysis.

Just a few remarks:

As in the xml schema, a hyperlink may have multiple runs. In some use cases, we have different styles for the text in the same link(see my response above). I am not sure how much extra work it requires to implement this. However, if we make a bit more effects at the beginning, we have docx api more flexible(as it adapts the xml schema) and we have more possibilties for hyperlink stylings.

You are right about the hyperlink access problem. I find Paragraph.iter_run_level_items() a bit confusing (see run level content) and I name the function as Paragraph.iter_p_content() in the Candidate protocol part. In fact I am bad at the naming thing. If you have an idea, please let me know. :)

I am not sure about the external/anchor link as it refers to a bookmark and so a bookmark should be implemented first. External links may be implemented later.

@scanny
Copy link
Contributor

scanny commented Mar 14, 2016

@tanyunshi -- Just wanted to mention to procedure for updating pull requests. To make changes, you do not create a new pull request (most folks seem to do this the first time :). What you do is update the branch you created the pull request with and then push that up to GitHub. The pull request is automatically updated.

That said, in this case, you'll need to create a new pull request because your pull is based on the master branch, and is also based on a different fork (robertdodd/python-docx). So you'll need to get your work into a new branch named 'feature/hyperlink' that's based on the 'feature/hyperlink' branch I've created on python-openxml. If you need help with the rebasing involved, let me know.

There are other challenges with making updates to pull requests, but we can face those when we come to them.

How familiar are you with git rebasing and with the GitHub pull process? I don't want to bore you with basics if you've already mastered them :)

@scanny
Copy link
Contributor

scanny commented Mar 14, 2016

As in the xml schema, a hyperlink may have multiple runs. In some use cases, we have different styles for the text in the same link(see my response above). I am not sure how much extra work it requires to implement this. However, if we make a bit more effects at the beginning, we have docx api more flexible(as it adapts the xml schema) and we have more possibilties for hyperlink stylings.

I can see your use case now. It's definitely legitimate, so we'll take a look and see how much work is involved. We'll need to look at the XML Schema for the hyperlink element and then should have an idea.

You are right about the hyperlink access problem. I find Paragraph.iter_run_level_items() a bit confusing (see run level content) and I name the function as Paragraph.iter_p_content() in the Candidate protocol part. In fact I am bad at the naming thing. If you have an idea, please let me know. :)

.iter_run_level_items() is the right name for that method if we implement it. There is already an .iter_block_level_items() method in the works on Document, and this would be analogous. The main question is what it should do with all the possible items it finds. You can see there are many possible child elements, of which run and hyperlink are just two (http://python-docx.readthedocs.org/en/latest/dev/analysis/schema/ct_p.html). The items that may appear within a run is something else altogether; I'm not sure what we would name that :)

For the moment I'm thinking .iter_run_level_items() would generate items of three possible types: Run, Hyperlink, and RunLevelItem. The first two would be a run and a hyperlink respectively. The last one would be used for everything else and only give access to the element (probably as .element). As other run-level items were implemented, they could be added explicitly. I'm inclined to think that Run and Hyperlink should inherit from RunLevelItem, but we can get to that later.

I am not sure about the external/anchor link as it refers to a bookmark and so a bookmark should be implemented first. External links may be implemented later.

I don't get what you're saying here. Can you elaborate?

@tanyunshi
Copy link
Author

I am not sure about the external/anchor link as it refers to a bookmark and so a bookmark should be implemented first. External links may be implemented later.

I don't get what you're saying here. Can you elaborate?

Good evening @scanny ,

A hyperlink may be an external link, such as url, or an anchor link, such as an bookmark. In the second cas, the link does not have an r:id attribute that refers to an element in relationship. However, the link has an attribute anchor, that refers to a name of a bookmark, and the bookmark can be found somewhere in the document.

I don't think we have bookmark in python-docx.

Therefore, I remove everything about anchor links in the analyses part and think about it later. :)

@tanyunshi
Copy link
Author

@scanny I close this PR as another one is available : #278

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants