-
Notifications
You must be signed in to change notification settings - Fork 1.2k
feature: Document.headers and footers #104
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
Comments
Hi @rotsee, @robline had been working on this feature over here: @robline: Can you give us an update on where you ended up and near-term prospects? |
That looks promising! Let me know if I can help somehow. |
I need this feature and am available to help too! |
hey Steve et al, I switched contracts so I haven't been working on it. I am slated to return to headers full time on Nov 26. I am happy to convo with anyone interested to talk through where my code is and look at next steps. I'd like to see this feature pushed through, and with a little effort it can be. I got the test framework running so now it is time to build failing tests and make them pass. I welcome anyone to take this and run with it, or assist me after the 26th, or whatever is best to get momentum. |
That is great @robline! I'll take a closer look at your branch and see where it goes. |
I picked up from your work, rebased to latest master and connected the dots here and there. Took me the whole day cause I wasn't familiar with the codebase. Anyway, it has no docs and no tests, but you can give it a try. See danmilon/python-docx#feature-headers. from docx import Document
d = Document('/tmp/doc_with_header.docx')
d.sections[0].headers[0].add_paragraph(text='moar header')
d.save('/tmp/moar_headers.docx') What do you think about the API? I'll add footer support tomorrow hopefully. |
Great, I try it out later this week! API looks fine, though |
Header and footer access from the section will need to be provided by a specific property for each of the three possible types. A simple sequence will not be articulate enough: Section.primary_header
Section.first_page_header
Section.even_page_header There also need to be a couple boolean properties on Section to determine whether the section has a different first-page header and whether it has distinct even page headers. Best practice is to document the proposed API on a page like this one (probably this specific one in this case), so it can be discussed and resolved before investing too much work in a wrong direction. See others here for more examples: This one might be a decent model: Microsoft API is usually a good-ish model to start with on API design. The API is much more important to get exactly right than its implementation. Reason being it's very rude to change it once it's published. Best to get that established early so your pull request doesn't require a lot of rework. |
That is right, thanks for the resources too! I'll do as you suggested. |
Could you help me a bit with the <w:settings>
...
<w:evenAndOddHeaders />
...
</w:settings> You also mentioned a flag that determines whether a secion has a different first-page header. Could you elaborate on that? |
I am away from my notes so this is from memory, and I will fill in specifics later using correct language, but... the headers part has 0-3 headers. Default header is the odd header. If there is an evenAndOddHeaders flag set, there should be two headers, one indicated as even and one as odd. There is also a third possible header, firstpage header, which overrides the odd header for the first page. I believe this same pattern can be applied to any of the document sections (of which there is always one by default, but could be many.) So my GUESS is that w:evenAndOddHeaders should be specified in the section. I am back on this project starting Monday, so I'm happy to convo/code/whatever starting then. |
@danmilon, if you want to get a good feel for how the XML is laid out for various situations, opc-diag is probably a good tool to get familiar with. You can make a quick example Word file then browse the XML it produces, and then compare that to other examples you make, like with an even page header, etc. I've always found the spec the barest of help in working through the initial analysis to resolve what behavior a feature should implement. I usually check it out anyway, but I usually get a lot more from the XML schema and inspecting Word-generated XML. |
Hi! First of all, thanks for this great python lib. Is there any estimation time of when this feature for header/footer adding will be available? |
Hi Vadim, well, we don't exactly have a development schedule because it's all based on when someone wants it bad enough to put in the work (or fund it). But this one is hovering near the top of the backlog. I'd say it's likely within about four to six months. |
Hi, Any news on this issue? someone have a fork that header and footer works? []s |
@scanny , Steve, it doesn't look like the work of bjinwright/python-docx or mikemaccana/python-docx have been merged into master here. Is that perhaps because adequate testing and regression have not been performed on their respectve commits, or because of higher-priority items on this project's backlog. Thanks! |
The mikemaccana repo is a different code base althogether (although we kept the name), so that will never be merged. Mostly this version is a superset of the legacy version; any remaining features would have to be developed for this code base. The bjinwright fork has only a single additional commit on it, and no tests I can see, so there would need to be quite a bit more work there to get the feature(s) it's implementing into shape for a merge. |
Hi @scanny! |
@scanny, still hoping that this is something that comes in 2016! For now, I'll work on a workaround. |
@AlbinoShadow I want to replace docx's header's text. |
I have lots of docx, and I want to replace these docx header's text Thanks |
This feature set is under active development by @eupharis on this pull request: #291 I don't believe there's a workaround at the moment, it would need to be a pretty hairy one given the involvement of new parts, but would certainly be possible. You can look at the analysis documents on that branch to get an idea what's involved. And of course encouragement/+1s on that PR is always helpful in keeping spirits high as we move the development along :) |
Thank you for your detailed answer. |
I am trying to add header to my document using docx-python. from docx import Document I am getting an error :AttributeError: 'Section' object has no attribute 'headers' Please help me out. Thanks in advance |
@mayuryeola this feature is being developed by @eupharis which can be seen under the pull request #291 as @scanny has stated in an above comment. @scanny, after reviewing the pull request #291, it seems as if the feature just needs to be merged with the master project? Please correct me if otherwise, I would like to use this as I am using a work around with pywin32 at the moment. Thank you to all whom have contributed to the development of this project, especially you @scanny, thanks again :) |
@Sum4196 that PR is work in progress. It doesn't work quite yet but it's probably about half-way to doing the basics. Might be done in a month or so, just guessing :) |
Sounds great :) I can't wait to see it implemented. It will be very useful to me for a program I'm building. This is the first large program I'm building and if any help is needed for this I would gladly devote my time to it!! |
hi there, is it done yet? I just download version 0.8.6, and when i use "section.header", it broke with "'Section' object has no attribute 'header'". I found another work around says "document.xpath(...)" like here or here. with 0.8.6, there is no document._document_part nor document.xpath existed. Please give me a suggestion what i can get a header of exist docx by far? Many thanks! |
@HuangKBAaron , below is a function that I gathered up from other sources and created a few elements that I have used in a recent project. This creates a header and footer on every page in the document. I do not know how to put them on just the first page, but you can figure that out I would like to know. Hopefully this helps you in your situation while the header and footer feature is integrated into python-docx. Thanks for your hard work @scanny and @eupharis . Note: For some reason the function won't copy into a single code block so it is split into two separate ones and will probably need to be copied one at a time. Also, this will only work on Windows as it communicates using COM.
|
Hi guy, thanks for all your hard work. is there a release date for the PR to be merged? |
@neoyagami The only way features get a delivery date is if someone sponsors them. Contributed features are just that, and depend on the contributor's timetable. |
My client might be willing to sponsor the effort to get this "over the finish line". Any idea how much would be required? |
@ccurvey I sent you an email to the email address on your profile. Let me know if you don't get it and we'll find another way to connect. |
Is PR merged after 2 years or its still pending? |
@baltechies work on this stalled at perhaps 30% of completion. If someone wants to pick it up or sponsor the feature it could probably be completed in a few weeks. Let me know if you're interested. |
I could probably chip in if some $$ would help. Any idea how many $$ it
will take to get it done?
…On Nov 14, 2017 4:43 PM, "Steve Canny" ***@***.***> wrote:
@baltechies <https://github.com/baltechies> work on this stalled at
perhaps 30% of completion. If someone wants to pick it up or sponsor the
feature it could probably be completed in a few weeks. Let me know if
you're interested.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#104 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAXiKZKPgQZcyd6qt0PNhXSQfa8YRMxyks5s2gmZgaJpZM4C084_>
.
|
Oh man, I offered to help a while ago, and the boss put the kibosh on it.
Let me see if I can re-raise the issue with him.
…On Tue, Nov 14, 2017 at 4:51 PM, Chris Curvey ***@***.***> wrote:
I could probably chip in if some $$ would help. Any idea how many $$ it
will take to get it done?
On Nov 14, 2017 4:43 PM, "Steve Canny" ***@***.***> wrote:
> @baltechies <https://github.com/baltechies> work on this stalled at
> perhaps 30% of completion. If someone wants to pick it up or sponsor the
> feature it could probably be completed in a few weeks. Let me know if
> you're interested.
>
> —
> You are receiving this because you were mentioned.
> Reply to this email directly, view it on GitHub
> <#104 (comment)>,
> or mute the thread
> <https://github.com/notifications/unsubscribe-auth/AAXiKZKPgQZcyd6qt0PNhXSQfa8YRMxyks5s2gmZgaJpZM4C084_>
> .
>
--
Proud to be a deplorable, bitter clinger.
|
@scanny I am able to resolve my issue using empty template for doc file. Otherwise this library is awesome to create a doc file. |
Hello guys. I am new in contribute of GIT projects but I want to help to resolve this issue. Yesterday I forked project and started working on this issue. I already implemented write to header and footer.
but my code is not covered by tests, yet. |
Thanks you @ondrej-111 : I'm using your fork to clear footers in a bunch of docx files. It works well and you saved me lot of time ! |
Thank you @Lokkook. Now I reworked my fork by this specification:
If somebody want to work with python-docx like in spec it is possible on this branch:
of my fork. I don't want to mess it up with master branch yet because somebody can using it... Next week I will implement tests and create PR. |
@ondrej-111 I am able to edit any existing header and footer of a document, however, creating them does not seem to work. |
Yes, sorry for misunderstanding It is not implemented yet. Only part with editing header, footers is working. At first I want to write some tests for this part and then add functionality to header, footer... |
I think it's a great start so far. I'm excited to see what you come up with in the future, thanks for the clarification. |
Header and footer support will be released with v0.8.8 in the next day or two. Until then a working version is available on the |
It would be incredibly useful to be able to get the page headers through Document.headers, or similar.
For now, I use a very ugly hack to get alll header texts:
The text was updated successfully, but these errors were encountered: