我正在使用Python-docx模块以编程方式创建word文档.
我希望能够将我的标题集中,在我创建的表格中将某些单词转换为粗体,并进行其他基本标记.
不幸的是,阅读模块中的源代码并没有给我带来很大的帮助.
我猜这与docx代码所基于的lxml/etree模块有关,但我对该库并不太熟悉.有任何想法吗?
我目前正在编写一个python 3程序,它解析某些docx文件并从中提取文本和图像.我一直在尝试使用docx,但它不会导入我的程序.我已经安装了lxml,Pillow和python-docx但它没有导入.当我尝试从终端使用python-docx时,我无法使用example-extracttext.py或example-makedocument.py,这让我相信安装没有正常运行.有没有办法可以检查这是否正确安装或是否有办法使其正常工作,以便我可以将其导入我的项目?我在Ubuntu 13.10上.
我正在尝试使用 python-docx 将图片插入到 Word 文档中,但遇到错误。
代码很简单:
document.add_picture("test.jpg", width = Cm(2.0))
Run Code Online (Sandbox Code Playgroud)
通过查看 python-docx 文档,我可以看到应该生成以下 XML:
<pic:pic xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture">
<pic:nvPicPr>
<pic:cNvPr id="1" name="python-powered.png"/>
<pic:cNvPicPr/>
</pic:nvPicPr>
<pic:blipFill>
<a:blip r:embed="rId7"/>
<a:stretch>
<a:fillRect/>
</a:stretch>
</pic:blipFill>
<pic:spPr>
<a:xfrm>
<a:off x="0" y="0"/>
<a:ext cx="859536" cy="343814"/>
</a:xfrm>
<a:prstGeom prst="rect"/>
</pic:spPr>
</pic:pic>
Run Code Online (Sandbox Code Playgroud)
这实际上是在我的 document.xml 文件中生成的。(解压 docx 文件时)。然而,查看 OOXML 格式,我可以看到图像也应该保存在media文件夹下,并且关系应该映射在word/_rels/document.xml 中:
<Relationship Id="rId20"
Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image"
Target="media/image20.png"/>
Run Code Online (Sandbox Code Playgroud)
然而,这一切都没有发生,当我打开 Word 文档时,我遇到了“图片无法显示”占位符。
谁能帮助我了解发生了什么事?
看起来图像没有按应有的方式嵌入,我需要将其插入媒体文件夹并为其添加映射,但是作为一个记录良好的功能,这应该按预期工作。
更新:
使用空的 docx 文件对其进行测试,图像确实按预期添加,这让我相信它可能与 python-docx-template 库有关。(https://github.com/elapouya/python-docx-template)
它使用 python-docx 和 jinja 来允许模板功能,但运行和工作方式与 python-docx应该相同。我将图像添加到子文档中,然后将其插入到给定位置的完整文档中。
示例代码如下(来自 …
我使用了这段代码:
# open a document
doc = docx.Document()
# add a table to the end and create a reference variable
# extra row is so we can add the header row
t = doc.add_table(df.shape[0]+1, df.shape[1])
t.allow_autofit = True
t.style = 'TableGrid'
t.alignment=WD_TABLE_ALIGNMENT.CENTER
Run Code Online (Sandbox Code Playgroud)
哪里df有一些pandas DataFrame。但列不能自动调整。
我在更新由 Linux 上的python-docx生成的 docx 文件中的目录时遇到问题。一般来说,创建TOC并不难(感谢这个答案/sf/answers/3403559211/和这个线程https://github.com/python-openxml/python-docx/issues/36)
from docx.oxml.ns import qn
from docx.oxml import OxmlElement
paragraph = self.document.add_paragraph()
run = paragraph.add_run()
fldChar = OxmlElement('w:fldChar') # creates a new element
fldChar.set(qn('w:fldCharType'), 'begin') # sets attribute on element
instrText = OxmlElement('w:instrText')
instrText.set(qn('xml:space'), 'preserve') # sets attribute on element
instrText.text = 'TOC \o "1-3" \h \z \u' # change 1-3 depending on heading levels you need
fldChar2 = OxmlElement('w:fldChar')
fldChar2.set(qn('w:fldCharType'), 'separate')
fldChar3 = OxmlElement('w:t')
fldChar3.text = "Right-click …Run Code Online (Sandbox Code Playgroud) 我正在尝试安装docx包。但得到以下信息ImportError:
ImportError: cannot import name Document
因此,按照此处的建议,我尝试了:
pip install python-docx
但出现以下错误(python版本:2.7.15)
..
..
..
creating build/lib/docx/templates
copying docx/templates/default-header.xml -> build/lib/docx/templates
copying docx/templates/default-settings.xml -> build/lib/docx/templates
copying docx/templates/default-footer.xml -> build/lib/docx/templates
error: can't copy 'docx/templates/default-docx-template': doesn't exist or not a regular file
Command
/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python -u -c "import
setuptools,tokenize;__file__='/private/var/folders/0c/v_yb4q7n2h3fg94rlfrr165r0000gn/T/pip-build-ks26RP/python-docx/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n',
'\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /var/folders/0c/v_yb4q7n2h3fg94rlfrr165r0000gn/T/pip-1SQvtb-record/install-record.txt --single-version-externally-managed --compile" failed with error code 1 in /private/var/folders/0c/v_yb4q7n2h3fg94rlfrr165r0000gn/T/pip-build-ks26RP/python-docx/
Run Code Online (Sandbox Code Playgroud) 我执行以下操作:
from docx import Document
document = Document('text.docx')
document.paragraphs[42].text
Run Code Online (Sandbox Code Playgroud)
它给出了 ''我输入的任何数字,并且 for 循环查找和替换单词不起作用。但是如果我用 保存文档document.save('text2.docx'),则该文档不为空。
该文档相对较大,包含许多不同的格式、图像、表格、样式。
我的任务是查找并替换 docx 文档中的单词,并对以下单词进行一些更正,所以如果您建议使用其他工具,我会很高兴
我对 python-docx 比较陌生。我试图更改现有文档中表格的行距,但它更改了文档中所有表格的行距。
这是一个最小的、可重现的示例,从头开始创建一个包含三个表的文档:
from docx import Document
from docx.shared import Inches
from docx.shared import Pt
from docx.enum.text import WD_LINE_SPACING
document = Document()
# Some sample text to add to tables
records = (
(3, '101', 'Spam'),
(7, '422', 'Eggs'),
(4, '631', 'Spam, spam, eggs, and spam')
)
# Create table 0
table = document.add_table(rows=1, cols=3)
hdr_cells = table.rows[0].cells
hdr_cells[0].text = 'Qty'
hdr_cells[1].text = 'Id'
hdr_cells[2].text = 'Desc'
for qty, id, desc in records:
row_cells = table.add_row().cells
row_cells[0].text = str(qty) …Run Code Online (Sandbox Code Playgroud) 我使用 python-docx 生成 Microsoft Word 文档,其中包含带有图像的表格。以下代码块显示了将图像添加到表中的 for 循环:
row_num = 2
img_cnt = 0
for i, var in enumerate(img_list_obj):
img = "img/NO-" + str(technical_object) + "/" + img_list_obj[i]
img_cnt = img_cnt + 1
row = table.rows[row_num].cells
if (img_cnt % 2) != 0:
paragraph = row[0].paragraphs[0]
paragraph.alignment = WD_ALIGN_PARAGRAPH.CENTER
table.cell(row_num, 0).vertical_alignment = WD_ALIGN_VERTICAL.CENTER
run = paragraph.add_run()
run.add_picture(img, width=Cm(7.50), height=Cm(5.50))
else:
paragraph = row[1].paragraphs[0]
paragraph.alignment = WD_ALIGN_PARAGRAPH.CENTER
table.cell(row_num, 1).vertical_alignment = WD_ALIGN_VERTICAL.CENTER
run = paragraph.add_run()
run.add_picture(img, width=Cm(7.50), height=Cm(5.50))
if i + 1 != …Run Code Online (Sandbox Code Playgroud) python-docx ×10
python ×9
docx ×3
ms-word ×2
openxml ×2
image ×1
importerror ×1
lxml ×1
macos ×1
pandas ×1
python-2.7 ×1
xml ×1