python-docx

Iterating through a table importing images

ぃ、小莉子 提交于 2020-01-25 07:28:27
问题 Importing images using Python docx. Found some help from old post but unable to convert it to a 3 row, 2 col table. from docx import Document document = Document() tables = document.tables table = document.add_table(rows=1, cols=2) row_cells = table.add_row().cells **for i, image in enumerate(['image1.jpg', 'image2.jpg']): paragraph = row_cells[i].paragraphs[0]** run = paragraph.add_run() run.add_picture(image) document.save('doc.docx') I've adapted it to... document = Document() tables =

“Last modified by” (user name, not time) attribute for xlsx using Python

99封情书 提交于 2020-01-24 21:50:51
问题 I need to be able to view the "last modified by" attribute for xlsx files using Python. I've been able to do for docx files, and was hoping that the architecture would be similar enough to use on other Office applications, but unfortunately not. Does anybody know of a similar module for xlsx? This is the script to view the field using python-docx: from docx import Document import docx document = Document('mine.docx') core_properties = document.core_properties print(core_properties.last

How can I make table header cells both bold and underline in python?

耗尽温柔 提交于 2020-01-24 01:52:10
问题 I am creating a table using python 3.4 and I would like to make the header both bold and underline. The following code will make the header bold: table = document.add_table(rows=1, cols=3) hdr_cells = table.rows[0].cells hdr_cells[0].paragraphs[0].add_run('Date Filmed:').bold = True hdr_cells[2].paragraphs[0].add_run('Barcode Number:').bold = True If I change the 3rd line to: hdr_cells[0].paragraphs[0].add_run('Date Filmed:').underline = True it will make the text underlined, but not bold. It

Adding a link to a bookmark in MS Word using python docx library

五迷三道 提交于 2020-01-22 02:15:23
问题 I've used the code from an earlier question to create a hyperlink: Adding an hyperlink in MSWord by using python-docx I now want to create a link to a bookmark within the document, rather than an external hyperlink, but can't work out how to do it. Any ideas? 回答1: Never mind. Found a way, thanks to neilbilly at github: feature: Paragraph.add_hyperlink() #74 def add_link(paragraph, link_to, text): hyperlink = docx.oxml.shared.OxmlElement('w:hyperlink') hyperlink.set(docx.oxml.shared.qn('w

How can I insert a checkbox form into a .docx file using python-docx?

ぐ巨炮叔叔 提交于 2020-01-20 08:47:08
问题 I've been using python to implement a custom parser and use that parsed data to format a word document to be distributed internally. All of the formatting has been straightforward and easy so far but I'm completely stumped on how to insert a checkbox into individual table cells. I've tried using the python object functions within python-docx (using get_or_add_tcPr() , etc.) which causes MS Word to throw the following error when I try to open the file, "The file xxxx cannot be opened because

How to split text read from a docx file with Page breaks using python3 docx

依然范特西╮ 提交于 2020-01-16 17:20:00
问题 I have a word document(.docx file) consisting of 10 pages with 1 paragraph on each page where each page/paragraph is seperated by a pagebreak. I want to read the text in the docx file and split it with the page breaks. I am able to read the text with python-docx library but I am not sure how to split it with page break. I can see a similar question but it's solution was proposed using the old python-docx library. Here's the code for reading text from docx file : from docx import Document

table style KeyError: u“no style with name 'Table Grid'”

眉间皱痕 提交于 2020-01-16 08:55:19
问题 I trying to insert a table via python-docx but it give the error code is: #-*-coding:utf-8-*- import re import time import datetime import sys import os import csv from docx import Document import docx from docx import * from docx.oxml import OxmlElement from docx.oxml.ns import qn from docx import Document from docx.shared import Inches from docx.enum.text import WD_ALIGN_PARAGRAPH from docx.shared import Pt from docx.shared import Cm from docx import section from docx.enum.table import WD

How to retrieve paragraphs, tables and images(inline shapes) by document order in python using docx library

偶尔善良 提交于 2020-01-06 05:59:07
问题 The python docx library works with word documents. The below piece of code extracts all paragraphs and tables in document order and appends them to a list. def iter_block_items(parent): """ Yield each paragraph and table child within *parent*, in document order. Each returned value is an instance of either Table or Paragraph. *parent* would most commonly be a reference to a main Document object, but also works for a _Cell object, which itself can contain paragraphs and tables. """ if

Possible to Insert page in word document with python-docx?

别等时光非礼了梦想. 提交于 2020-01-05 04:51:26
问题 I just read through the documentation on python-docx. They mention several times that added content is created at the end of the document, but I didn't notice any way to alter this functionality. Does anyone know how to add a new page to a pre-existing document, but make it page 1? Thanks! 回答1: The short answer is the library doesn't support that just yet, although those features are high on the backlog so will be among the next to be implemented. To get it done in the meantime you'll need to

How to get an image (inlineshape) from paragraph python docx

你离开我真会死。 提交于 2020-01-04 06:06:46
问题 I want to read the docx document paragraph by paragraph and if there is a picture (InlineShape), then process it with the text around it. The function Document.inline_shapes will give the list of all inline shapes in the document. But I want to get the one, that appears exactly in the current paragraph if exists... An example of code: from docx import Document doc = Document("test.docx") blip = doc.inline_shapes[0]._inline.graphic.graphicData.pic.blipFill.blip rID = blip.embed document_part =