python-docx

Text-Replace in docx and save the changed file with python-docx

≯℡__Kan透↙ 提交于 2019-11-27 18:02:11
问题 I'm trying to use the python-docx module to replace a word in a file and save the new file with the caveat that the new file must have exactly the same formatting as the old file, but with the word replaced. How am I supposed to do this? The docx module has a savedocx that takes 7 inputs: document coreprops appprops contenttypes websettings wordrelationships output How do I keep everything in my original file the same except for the replaced word? 回答1: As it seems to be, Docx for Python is

Python: Create a “Table Of Contents” with python-docx/lxml

强颜欢笑 提交于 2019-11-27 16:49:32
问题 I'm trying to automate the creation of .docx files (WordML) with the help of python-docx (https://github.com/mikemaccana/python-docx). My current script creates the ToC manually with following loop: for chapter in myChapters: body.append(paragraph(chapter.text, style='ListNumber')) Does anyone know of a way to use the "word built-in" ToC-function, which adds the index automatically and also creates paragraph-links to the individual chapters? Thanks a lot! 回答1: The key challenge is that a

Page number python-docx

穿精又带淫゛_ 提交于 2019-11-27 14:48:55
I am trying to create a program in python that can find a specific word in a .docx file and return page number that it occurred on. So far, in looking through the python-docx documentation I have been unable to find how do access the page number or even the footer where the number would be located. Is there a way to do this using python-docx or even just python? Or if not, what would be the best way to do this? Short answer is no, because the page breaks are inserted by the rendering engine, not determined by the .docx file itself. However, certain clients place a <w:lastRenderedPageBreak>

How to extract text from an existing docx file using python-docx

痴心易碎 提交于 2019-11-27 11:47:31
I'm trying to use python-docx module ( pip install python-docx ) but it seems to be very confusing as in github repo test sample they are using opendocx function but in readthedocs they are using Document class. Even they are only showing how to add text to a docx file not reading existing one? 1st one ( opendocx ) is not working, may be deprecated. For second case I was trying to use: from docx import Document document = Document('test_doc.docx') print document.paragraphs It returned a list of <docx.text.Paragraph object at 0x... > Then I did: for p in document.paragraphs: print p.text It

Add an image in a specific position in the document (.docx) with Python?

依然范特西╮ 提交于 2019-11-27 02:43:16
问题 I use Python-docx to generate Microsoft Word document.The user want that when he write for eg: "Good Morning every body,This is my %(profile_img)s do you like it?" in a HTML field, i create a word document and i recuper the picture of the user from the database and i replace the key word %(profile_img)s by the picture of the user NOT at the END OF THE DOCUMENT . With Python-docx we use this instruction to add a picture: document.add_picture('profile_img.png', width=Inches(1.25)) The picture

Python docx Replace string in paragraph while keeping style

巧了我就是萌 提交于 2019-11-26 20:16:21
问题 I need help replacing a string in a word document while keeping the formatting of the entire document. I'm using python-docx, after reading the documentation, it works with entire paragraphs, so I loose formatting like words that are in bold or italics. Including the text to replace is in bold, and I would like to keep it that way. I'm using this code: from docx import Document def replace_string2(filename): doc = Document(filename) for p in doc.paragraphs: if 'Text to find and replace' in p

When import docx in python3.3 I have error ImportError: No module named &#39;exceptions&#39;

吃可爱长大的小学妹 提交于 2019-11-26 12:45:48
问题 when I import docx I have this error: >File \"/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/docx-0.2.4-py3.3.egg/docx.py\", line 30, in <module> from exceptions import PendingDeprecationWarning ImportError: No module named \'exceptions\' How to fix this error ( python3.3 , docx 0.2.4 )? 回答1: If you are using python 3x don't do pip install docx instead go for pip install python-docx it is compatible with python 3x official Document: https://pypi.org/project

How to extract text from an existing docx file using python-docx

帅比萌擦擦* 提交于 2019-11-26 10:56:41
问题 I\'m trying to use python-docx module ( pip install python-docx ) but it seems to be very confusing as in github repo test sample they are using opendocx function but in readthedocs they are using Document class. Even they are only showing how to add text to a docx file not reading existing one? 1st one ( opendocx ) is not working, may be deprecated. For second case I was trying to use: from docx import Document document = Document(\'test_doc.docx\') print document.paragraphs It returned a