python-docx

Header and footer creation in versions before 0.8.8

大兔子大兔子 提交于 2019-12-10 02:38:16
问题 Is there a workaround for adding headers and footers in a Microsoft Word ( docx ) file? These are not implemented in versions of python-docx prior to 0.8.8 . More specifically, I would like to add: Page number to footers Some random text to headers The ideal code will look as follows: from docx import Document document = Document() # Add header and footer on all pages document.save("demo.docx") 回答1: How about something like this (Thanks to Eliot K) from docx import Document import win32com

Parsing of table from .docx file [closed]

狂风中的少年 提交于 2019-12-09 11:01:15
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 4 years ago . I want to parse a table from a .docx file using Python and python-docx into some useful data structure. The .docx file contains only a single table in my case. I've uploaded it so you can have a look. Here's a screenshot: 回答1: You can use the snippet below to parse your document into a list where

Read and Write .docx file with python

江枫思渺然 提交于 2019-12-09 07:51:41
问题 I have a folder containing several .docx files with names [Code2001.docx, Code2002.docx... Code2154.docx] . I'm trying to write a script that will: Open each .docx file Append one line to the document; "This is checked" Save the .docx-file to another folder, named "Code2001_checked" After searching I've only managed to get the filename with the loop: import os os.chdir(r"E:......\test") for files in os.listdir("."): if files.endswith(".docx"): print filename I also found this: docx module but

Python-docx: Is it possible to add a new run to paragraph in a specific place (not at the end)

╄→尐↘猪︶ㄣ 提交于 2019-12-08 07:30:54
问题 I want to set a style to a corrected word in MS Word text. Since it's not possible to change text style inside a run, I want to insert a new run with new style into the existing paragraph... for p in document.paragraphs: for run in p.runs: if 'text' in run.text: new_run= Run() new_run.text='some new text' # insert this run into paragraph # smth like: p.insert(new_run) How to do it? p.add_run() adds run to the end of paragraph, doesn't it? Update The best would be to be able to clone run (and

Missing document text when using python-docx

人走茶凉 提交于 2019-12-08 06:15:33
问题 I am using python-docx 0.8.6 and python 3.6 to preform a simple search/replace operation. I'm having a problem where not all of the document's text appears when iterating over the doc.paragraphs For debugging I have tried doc = Document(input_file) fullText = [] for para in doc.paragraphs: fullText.append(para.text) print('\n'.join(fullText)) Which only seems to print out about half of the file's contents. There are no tables or special formatting in the file. Is there any reason why so much

How can I add page numbers to each page's footer with python-docx?

强颜欢笑 提交于 2019-12-08 03:09:49
问题 I think this question is pretty self explanatory. From what I've read of the python-docx documentation, it seems that the header and footer must be exactly the same on every page, which of course makes adding page numbers difficult. Is this possible? 回答1: Adding headers and footers is a feature not yet implemented. However... If it is an existing document you want to add headers and footers to you can call a VBA-macro. I recently posted a way to do that (https://stackoverflow.com/a/44767400

Extracting MS Word document formatting elements along with raw text information

烈酒焚心 提交于 2019-12-08 02:22:13
问题 In this post @mikemaccana describes how to use python-docx to extract raw text data from an MS Word document from within python. I'd like to go one step further. Instead of simple extracting the raw text information, can I also use this module to harvest information about font face (e.g. bold versus italic) or font size (e.g. 12 versus 18pt). The closest I was able to come was this post asking about using this module to extract highlighted text entries. Seemed a little abstract, and I'm not

python-docx add_style with CTL (Complex text layout) language

六眼飞鱼酱① 提交于 2019-12-08 00:20:57
问题 What I’m trying to accomplish: Create a paragraph style in python-docx with user defined Persian font and size (a CTL language) Problem: I can do this with non-CTL languages like English: from docx import Document from docx.enum.style import WD_STYLE_TYPE from docx.shared import Pt user_font_name = 'FreeMono' user_font_size = 14 doc = Document() my_style = doc.styles.add_style('style_name',WD_STYLE_TYPE.PARAGRAPH) my_font = my_style.font my_font.name = user_font_name my_font.size = Pt(user

highlight text using python-docx

Deadly 提交于 2019-12-07 22:48:01
问题 I want to highlight text in docx and save it another file. here is my code from docx import Document def highlight_text(filename): doc = Document(filename) for p in doc.paragraphs: if 'vehicle' in p.text: inline = p.runs # print(inline) # Loop added to work with runs (strings with same style) for i in range(len(inline)): # print((inline[i].text).encode('ascii')) if 'vehicle' in inline[i].text: x=inline[i].text.split('vehicle') inline[i].clear() for j in range(len(x)-1): inline[i].add_text(x[j

Search and Replace in python-docx

与世无争的帅哥 提交于 2019-12-07 18:26:32
问题 I have a document (template) with the following string: "Hello, my name is Bob. Bob is a nice name." I would like to open this document using python-docx and use "find and replace" method (if exists) to change every single string "Bob" -> "Mark". At the end I would like to generate a new document with a string "Hello, my name is Mark. Mark is a nice name." How can I do that? from docx import * TEMPLATE_FILE = 'test_template.docx' class generate_docx: @staticmethod def test(): document =