python-docx

Write text in particular font color in MS word using python-docx

守給你的承諾、 提交于 2019-11-29 09:53:39
I am trying to write text in an MS Word file using python library python-docx. I have gone through the documentation of python-docx's font color on this link and applied the same in my code, but am unsuccessful so far. Here is my code: from docx import Document from docx.shared import RGBColor document = Document() run = document.add_paragraph('some text').add_run() font = run.font font.color.rgb = RGBColor(0x42, 0x24, 0xE9) p=document.add_paragraph('aaa') document.save('demo1.docx') The text in word file 'demo.docx' is simply in black color. I am not able to figure this out, help would be

How to iterate over everything in a python-docx document?

这一生的挚爱 提交于 2019-11-29 06:56:53
I am using python-docx to convert a Word docx to a custom HTML equivalent. The document that I need to convert has images and tables, but I haven't been able to figure out how to access the images and the tables within a given run. Here is what I am thinking... for para in doc.paragraphs: for run in para.runs: # How to tell if this run has images or tables? ...but I don't see anything on the Run that has info on the InlineShape or Table . Do I have to fall back to the XML directly or is there a better, cleaner way to iterate over everything in the document? Thanks! There are actually two

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

不问归期 提交于 2019-11-29 04:14:03
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? edi9999 As it seems to be, Docx for Python is not meant to store a full Docx with images, headers, ... , but only contains the inner content of the

When import docx in python3.3 I have error ImportError: No module named 'exceptions'

柔情痞子 提交于 2019-11-29 00:51:09
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 )? Arun 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/python-docx/ Uninstall docx module with pip uninstall docx Download python_docx-0.8.6-py2.py3-none-any.whl file

Write text in particular font color in MS word using python-docx

喜夏-厌秋 提交于 2019-11-28 03:12:12
问题 I am trying to write text in an MS Word file using python library python-docx. I have gone through the documentation of python-docx's font color on this link and applied the same in my code, but am unsuccessful so far. Here is my code: from docx import Document from docx.shared import RGBColor document = Document() run = document.add_paragraph('some text').add_run() font = run.font font.color.rgb = RGBColor(0x42, 0x24, 0xE9) p=document.add_paragraph('aaa') document.save('demo1.docx') The text

Python-docx, how to set cell width in tables?

送分小仙女□ 提交于 2019-11-28 02:49:02
问题 How to set cell width in tables?, so far I got: from docx import Document from docx.shared import Cm, Inches document = Document() table = document.add_table(rows=2, cols=2) table.style = 'TableGrid' #single lines in all cells table.autofit = False col = table.columns[0] col.width=Inches(0.5) #col.width=Cm(1.0) #col.width=360000 #=1cm document.save('test.docx') No mater what number or units I set in col.width, its width does not change. 回答1: Short answer: set cell width individually. for cell

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

天涯浪子 提交于 2019-11-27 22:24:47
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 is added to the document but the problem that it is added at the end of the document. Is it impossible

Python docx Replace string in paragraph while keeping style

混江龙づ霸主 提交于 2019-11-27 20:18:44
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.text: print 'SEARCH FOUND!!' text = p.text.replace('Text to find and replace', 'new text') style = p

Set paragraph font in python-docx

前提是你 提交于 2019-11-27 18:22:31
问题 I am using python-docx 0.7.6. I can't seem to be able to figure out how to set font family and size for a certain paragraph. There is .style property but style="Times New Roman" doesn't work. Can somebody please point me to an example? Thanks. 回答1: This is how to set the Normal style to font Arial and size 10pt . from docx.shared import Pt style = document.styles['Normal'] font = style.font font.name = 'Arial' font.size = Pt(10) And this is how to apply it to a paragraph . paragraph.style =

combine word document using python docx

99封情书 提交于 2019-11-27 18:21:15
问题 I have few word files that each have specific content. I would like for a snippet that show me or help me to figure out how to combine the word files into one file, while using Python docx library. For example in pywin32 library I did the following: rng = self.doc.Range(0, 0) for d in data: time.sleep(0.05) docstart = d.wordDoc.Content.Start self.word.Visible = True docend = d.wordDoc.Content.End - 1 location = d.wordDoc.Range(docstart, docend).Copy() rng.Paste() rng.Collapse(0) rng