Add Line Numbers Programatically toHow can I do a line break (line continuation) in Python?How do I check if a string is a number (float)?Add new keys to a dictionary?How do I get the number of elements in a list?How to read a file line-by-line into a list?Correct way to write line to file?Catch multiple exceptions in one line (except block)Why is reading lines from stdin much slower in C++ than Python?OpenXml: Programmatically get NumberingRestart settings for FootnotesHow to add line numbers to a docx document section using python-docx

Codewars - Highest Scoring Word

Under GDPR, can I give permission once to allow everyone to store and process my data?

Did the Apollo Guidance Computer really use 60% of the world's ICs in 1963?

Can a network vulnerability be exploited locally?

Why did Starhopper's exhaust plume become brighter just before landing?

Why are JWST optics not enclosed like HST?

Why is "I let him to sleep" incorrect (or is it)?

Printing a list as "a, b, c." using Python

Defending Castle from Zombies

Historical Daf Yomi calendar

Storing milk for long periods of time

Where should I draw the line on follow up questions from previous employer

Journal published a paper, ignoring my objections as a referee

How can I reply to coworkers who accuse me of automating people out of work?

Count the number of triangles

What is the practical impact of using System.Random which is not cryptographically random?

Why does Sauron not permit his followers to use his name?

Fixing a blind bolt hole when the first 2-3 threads are ruined?

Can this planet in a binary star system exist?

Why military weather satellites?

How can I improve my formal definitions

'Horseshoes' for Deer?

I feel cheated by my new employer, does this sound right?

Does Dovescape counter Enchantment Creatures?



Add Line Numbers Programatically to


How can I do a line break (line continuation) in Python?How do I check if a string is a number (float)?Add new keys to a dictionary?How do I get the number of elements in a list?How to read a file line-by-line into a list?Correct way to write line to file?Catch multiple exceptions in one line (except block)Why is reading lines from stdin much slower in C++ than Python?OpenXml: Programmatically get NumberingRestart settings for FootnotesHow to add line numbers to a docx document section using python-docx






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















I want to be able to programmatically add line numbers to the entire document. I stumbled upon python-docx which allows me to fiddle with .docx files in python. However, I was unable to successfully add line numbers to my document using the extension using the following code:



from docx import Document
from docx.oxml.shared import OxmlElement

document = Document('sample.docx')

sections = document.sections

for section in sections:
sectPr = section._sectPr

lnNumType = OxmlElement('w:lnNumType')
lnNumType.set('countBy', '1')
lnNumType.set('start', '1')
lnNumType.set('restart', 'newSection')
sectPr.append(lnNumType)

document.save('sample-output.docx')


When I open the generated sample-output.docx in Word, the line number does not show up. Yet when I convert the file to xml, I can see that <w:lnNumType w:count-by="5" w:distance="282.9954" w:restart="continuous"/> has been added to the document. Within the <w:wordDocument> and <w:body> xml tags.



I am not too sure if this is a problem with my code or with the way .docxs work that is causing this problem.



Hopefully, someone can help me out here or suggest other ways to do this.










share|improve this question


























  • The problem is that the command for line numbering needs to be in the document's section area, not in the document body: <w:sectPr>...<w:lnNumType>...</w:sectPar> Note that the element order is sometimes very strict in this area - it depends on what comes before and after. I recommend you create a sample document as a user, activate line numbering, save it, then inspect the XML.

    – Cindy Meister
    Mar 28 at 15:41

















0















I want to be able to programmatically add line numbers to the entire document. I stumbled upon python-docx which allows me to fiddle with .docx files in python. However, I was unable to successfully add line numbers to my document using the extension using the following code:



from docx import Document
from docx.oxml.shared import OxmlElement

document = Document('sample.docx')

sections = document.sections

for section in sections:
sectPr = section._sectPr

lnNumType = OxmlElement('w:lnNumType')
lnNumType.set('countBy', '1')
lnNumType.set('start', '1')
lnNumType.set('restart', 'newSection')
sectPr.append(lnNumType)

document.save('sample-output.docx')


When I open the generated sample-output.docx in Word, the line number does not show up. Yet when I convert the file to xml, I can see that <w:lnNumType w:count-by="5" w:distance="282.9954" w:restart="continuous"/> has been added to the document. Within the <w:wordDocument> and <w:body> xml tags.



I am not too sure if this is a problem with my code or with the way .docxs work that is causing this problem.



Hopefully, someone can help me out here or suggest other ways to do this.










share|improve this question


























  • The problem is that the command for line numbering needs to be in the document's section area, not in the document body: <w:sectPr>...<w:lnNumType>...</w:sectPar> Note that the element order is sometimes very strict in this area - it depends on what comes before and after. I recommend you create a sample document as a user, activate line numbering, save it, then inspect the XML.

    – Cindy Meister
    Mar 28 at 15:41













0












0








0


0






I want to be able to programmatically add line numbers to the entire document. I stumbled upon python-docx which allows me to fiddle with .docx files in python. However, I was unable to successfully add line numbers to my document using the extension using the following code:



from docx import Document
from docx.oxml.shared import OxmlElement

document = Document('sample.docx')

sections = document.sections

for section in sections:
sectPr = section._sectPr

lnNumType = OxmlElement('w:lnNumType')
lnNumType.set('countBy', '1')
lnNumType.set('start', '1')
lnNumType.set('restart', 'newSection')
sectPr.append(lnNumType)

document.save('sample-output.docx')


When I open the generated sample-output.docx in Word, the line number does not show up. Yet when I convert the file to xml, I can see that <w:lnNumType w:count-by="5" w:distance="282.9954" w:restart="continuous"/> has been added to the document. Within the <w:wordDocument> and <w:body> xml tags.



I am not too sure if this is a problem with my code or with the way .docxs work that is causing this problem.



Hopefully, someone can help me out here or suggest other ways to do this.










share|improve this question
















I want to be able to programmatically add line numbers to the entire document. I stumbled upon python-docx which allows me to fiddle with .docx files in python. However, I was unable to successfully add line numbers to my document using the extension using the following code:



from docx import Document
from docx.oxml.shared import OxmlElement

document = Document('sample.docx')

sections = document.sections

for section in sections:
sectPr = section._sectPr

lnNumType = OxmlElement('w:lnNumType')
lnNumType.set('countBy', '1')
lnNumType.set('start', '1')
lnNumType.set('restart', 'newSection')
sectPr.append(lnNumType)

document.save('sample-output.docx')


When I open the generated sample-output.docx in Word, the line number does not show up. Yet when I convert the file to xml, I can see that <w:lnNumType w:count-by="5" w:distance="282.9954" w:restart="continuous"/> has been added to the document. Within the <w:wordDocument> and <w:body> xml tags.



I am not too sure if this is a problem with my code or with the way .docxs work that is causing this problem.



Hopefully, someone can help me out here or suggest other ways to do this.







python ms-word openxml python-docx






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 28 at 15:39









Cindy Meister

18.2k10 gold badges25 silver badges39 bronze badges




18.2k10 gold badges25 silver badges39 bronze badges










asked Mar 27 at 22:14









PabiPabi

4382 gold badges10 silver badges30 bronze badges




4382 gold badges10 silver badges30 bronze badges















  • The problem is that the command for line numbering needs to be in the document's section area, not in the document body: <w:sectPr>...<w:lnNumType>...</w:sectPar> Note that the element order is sometimes very strict in this area - it depends on what comes before and after. I recommend you create a sample document as a user, activate line numbering, save it, then inspect the XML.

    – Cindy Meister
    Mar 28 at 15:41

















  • The problem is that the command for line numbering needs to be in the document's section area, not in the document body: <w:sectPr>...<w:lnNumType>...</w:sectPar> Note that the element order is sometimes very strict in this area - it depends on what comes before and after. I recommend you create a sample document as a user, activate line numbering, save it, then inspect the XML.

    – Cindy Meister
    Mar 28 at 15:41
















The problem is that the command for line numbering needs to be in the document's section area, not in the document body: <w:sectPr>...<w:lnNumType>...</w:sectPar> Note that the element order is sometimes very strict in this area - it depends on what comes before and after. I recommend you create a sample document as a user, activate line numbering, save it, then inspect the XML.

– Cindy Meister
Mar 28 at 15:41





The problem is that the command for line numbering needs to be in the document's section area, not in the document body: <w:sectPr>...<w:lnNumType>...</w:sectPar> Note that the element order is sometimes very strict in this area - it depends on what comes before and after. I recommend you create a sample document as a user, activate line numbering, save it, then inspect the XML.

– Cindy Meister
Mar 28 at 15:41












0






active

oldest

votes










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55387279%2fadd-line-numbers-programatically-to%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes




Is this question similar to what you get asked at work? Learn more about asking and sharing private information with your coworkers using Stack Overflow for Teams.







Is this question similar to what you get asked at work? Learn more about asking and sharing private information with your coworkers using Stack Overflow for Teams.



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55387279%2fadd-line-numbers-programatically-to%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript