我想打开一个现有的 Word 文档,我已经在其中添加了页码,然后向其中添加了一些文本和标题。
这是我如何尝试实现目标的基本示例
#!/usr/bin/env python
from docx import Document
document = Document('report-template.docx')
document.add_heading('Headline No. 1', level=1)
document.add_paragraph('Test No. 1')
document.add_heading('Heading No. 2', level=2)
document.add_paragraph('Test No. 2')
document.save('example.docx')
Run Code Online (Sandbox Code Playgroud)
当我使用完整的新文档执行上述操作时,一切正常 - 当使用现有文件执行此操作时,它会失败并出现以下错误
Traceback (most recent call last):
File "create-report-test.py", line 6, in <module>
document.add_heading('Headline No. 1', level=1)
File "/usr/lib/python2.7/site-packages/docx/document.py", line 43, in add_heading
return self.add_paragraph(text, style)
File "/usr/lib/python2.7/site-packages/docx/document.py", line 63, in add_paragraph
return self._body.add_paragraph(text, style)
File "/usr/lib/python2.7/site-packages/docx/blkcntnr.py", line 38, in add_paragraph
paragraph.style = style
File "/usr/lib/python2.7/site-packages/docx/text/paragraph.py", line 111, in style
style_or_name, …Run Code Online (Sandbox Code Playgroud) 下面的代码片段基本上创建了一个表格,其中包含新 Word 文档中所需的行数和列数,即 2 列和 14 行。然后相应地将内容添加到行和列。
from docx import Document
newDoc=Document()
newDoc.add_heading ('GIS Request Form')
newDoc.add_paragraph()
#inserting a table and the header and value objects to the table
table=newDoc.add_table(rows=14,cols=2)
table.style='Table Grid'
table.autofit=False
table.columns[0].width=2500000
table.columns[1].width=3500000
#inserting contents into table cells
for i in range(0,14):
row=table.rows[i]
row.cells[0].text=reqdheaderList[i]
row.cells[1].text=reqdvalueList[i]
Run Code Online (Sandbox Code Playgroud)
我一直试图将第 1 列中所有内容的内容加粗,但它不起作用。
#inserting contents into table cells
for i in range(0,14):
row=table.rows[i]
row.cells[0].text=reqdheaderList[i]
row.cells[0].paragraphs[0].add_run(line[0]).bold=True
row.cells[1].text=reqdvalueList[i]
Run Code Online (Sandbox Code Playgroud)
帮助?
我使用 python-docx 中的示例,运行代码后我找不到 docx 文件在哪里,我可以指出我想要添加的特定路径吗?
from docx import Document
from docx.shared import Inches
document = Document('C:\Users\Administrator\Desktop\python test\update_test\\test.docx')
document.add_heading('Document Title', 0)
p = document.add_paragraph('A plain paragraph having some ')
p.add_run('bold').bold = True
p.add_run(' and some ')
p.add_run('italic.').italic = True
document.add_heading('Heading, level 1', level=1)
document.add_paragraph('Intense quote', style='Intense Quote')
document.add_paragraph(
'first item in unordered list', style='List Bullet'
)
document.add_paragraph(
'first item in ordered list', style='List Number'
)
filename='test.docx'
filepath=r'C:\Users\Administrator\Desktop\python test\update_test'+filename
document.add_page_break()
document.save(filepath)
Run Code Online (Sandbox Code Playgroud) 我想使用 python-docx 访问具有唯一字体或字体大小的段落或运行。我如何获取具有指定字体或字体大小的运行或段落的文本?
我正在尝试使用 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。但列不能自动调整。
我知道我错过了一些简单的东西,但它并没有陷入困境。我知道我必须在每个单独的单元格上设置宽度。
我想在 Word docx 中构建一个表格,其中第一列是 1.2 英寸,第二列是 5.3 英寸。当我尝试以下操作时,第一列是 0.63 英寸,第二列是 1.72 英寸。我为宽度尺寸设置了什么似乎并不重要。我在第一列尝试了 3.0,它仍然显示为 0.63 英寸。我在这里缺少什么?
import docx
doc = docx.Document()
doc.add_heading('Name: ', level=1)
table = doc.add_table(rows=4, cols=2)
table.cell(0,0).width = 1.2
table.cell(1,0).width = 1.2
table.cell(2,0).width = 1.2
table.cell(3,0).width = 1.2
table.cell(0,1).width = 5.3
table.cell(1,1).width = 5.3
table.cell(2,1).width = 5.3
table.cell(3,1).width = 5.3
table.cell(0,0).text = 'Time Zone'
table.cell(1,0).text = 'Link'
table.cell(1,1).text = 'https://www.google.com/'
table.cell(2,0).text = 'Website'
table.cell(3,0).text = 'Facebook'
doc.save('test.docx')
Run Code Online (Sandbox Code Playgroud) 我希望每次运行代码时在 Word 文档中附加一个徽标文件,
理想情况下,代码应如下所示:
from docx import Document
document = Document()
logo = open('logo.eps', 'r') #the logo path that is to be attached
document.add_heading('Underground Heating Oil Tank Search Report', 0) #simple heading that will come bellow the logo in the header.
document.save('report for xyz.docx') #saving the file
Run Code Online (Sandbox Code Playgroud)
这在 python-docx 中可能吗?还是我应该尝试其他库来做到这一点?如果可能的话请告诉我怎么做
我正在使用该python-docx库来提取 ms word 文档。我可以使用相同的库从 word 文档中获取所有表格。但是,我想将表解析为熊猫数据框,是否有任何内置功能可用于将表解析为数据框,或者我必须手动执行此操作?另外,是否有可能知道表格所在的标题名称?谢谢
from docx import Document
from docx.shared import Inches
document = Document('test.docx')
tabs = document.tables
Run Code Online (Sandbox Code Playgroud) python-docx ×10
python ×9
docx ×4
ms-word ×2
pandas ×2
dataframe ×1
python-3.6 ×1
python-3.x ×1