我正在使用python库docx:http://github.com/mikemaccana/python-docx
我的目标是打开一个文件,替换某些单词,然后用替换文件写入该文件.
我目前的代码:
#! /usr/bin/python
from docx import *
openDoc = "test.docx"
writeDoc = "test2.docx"
replace = {"Test":"TEST"}
document = opendocx(openDoc)
docbody = document.xpath('/w:document/w:body', namespaces=nsprefixes)[0]
print getdocumenttext(document)
for key in replace:
if search(docbody, key):
print "Found" , key , "replacing with" , replace[key]
docbody = replace(docbody,key,replace[key])
print getdocumenttext(document)
# ideally just want to mirror current document details here..
relationships = relationshiplist()
coreprops = coreproperties(title='',subject='',creator='',keywords=[])
savedocx(document,coreprops,appproperties(),contenttypes(),websettings(),wordrelationships(relationships),'a.docx')
Run Code Online (Sandbox Code Playgroud)
`但是我收到以下错误:
Traceback (most recent call last):
File "process.py", line 17, in <module> …Run Code Online (Sandbox Code Playgroud) 我有一个文件夹,其中包含几个带有名称的.docx文件[Code2001.docx, Code2002.docx... Code2154.docx].
我正在尝试编写一个脚本:
搜索后,我只是设法获得循环文件名:
import os
os.chdir(r"E:......\test")
for files in os.listdir("."):
if files.endswith(".docx"):
print filename
Run Code Online (Sandbox Code Playgroud)
我也发现了这个:docx模块,但文档很难继续.
有关如何完成此脚本的任何建议?
from docx import *
document = Document('ABC.docx')
for paragraph in document.paragraphs:
for run in paragraph.runs:
if run.style == 'Strong':
print run.text
Run Code Online (Sandbox Code Playgroud)
这是我用来打开docx文件并检查是否有粗体文本但我没有得到任何结果的代码.如果我删除if语句,则打印整个文件时没有任何格式/样式.你能告诉我如何使用python-docx识别像Bold或Italics这样的特定文本的文本吗?谢谢
我正在使用 python-docx 将 Pandas DataFrame 输出到 Word 表。大约一年前,我编写了这段代码来构建那个当时有效的表:
table = Rpt.add_table(rows=1, cols=(df.shape[1]+1))
table.style = 'LightShading-Accent2'
Run Code Online (Sandbox Code Playgroud)
Rpt模板中的文档在哪里。现在,我收到一个错误:
KeyError: "no style with name 'LightShading-Accent2'"
Run Code Online (Sandbox Code Playgroud)
我应该如何定义风格?较新版本的 python-docx 中的命名约定是否发生了变化?
我正在写一本字典.我正在使用python-docx将其放入MS Word中.我可以很容易地使它变得粗体或斜体,但似乎无法弄清楚如何做到这两点.这是基础知识:
import docx
word = 'Dictionary'
doc = docx.Document()
p = doc.add_paragraph()
p.add_run(word).bold = True
doc.save('test.docx')
Run Code Online (Sandbox Code Playgroud)
我试过p.add_run(word).bold.italic = True,但收到'NoneType'错误,我明白了.
我也在add_run之前和之后尝试了p.bold = True和p.italic = True,但是一起丢失了格式.
Word的查找/替换是一个简单的解决方案,但如果可以,我宁愿在代码中执行此操作.
我一直在尝试使用以下方法在MS Word文档中找到突出显示的颜色 python-docx(python-docx-0.8.6,python 2.7、32位)并根据其突出显示颜色处理文本的每一部分。
根据文档,我尝试导入/使用WD_COLOR_INDEX,但似乎找不到它。
from docx.enum import *
if (doc.paragraphs[i].runs[j].font.highlight_color == WD_COLOR_INDEX.YELLOW):
#do the appropriate thing for the yellow-highlighted text
Run Code Online (Sandbox Code Playgroud)
如何导入颜色索引?
我有一堆docx具有相同嵌入 Excel 表格的 Word文件。我正在尝试从多个文件中提取相同的单元格。
我想出了如何硬编码到一个文件:
from docx import Document
document = Document(r"G:\GIS\DESIGN\ROW\ROW_Files\Docx\006-087-003.docx")
table = document.tables[0]
Project_cell = table.rows[2].cells[2]
paragraph = Project_cell.paragraphs[0]
Project = paragraph.text
print Project
Run Code Online (Sandbox Code Playgroud)
但是我该如何批处理呢?我在 上尝试了一些变体listdir,但它们对我不起作用,而且我太绿了,无法独自到达那里。
table = document.add_table(rows=1, cols=1)
hdr_cells = table.rows[0].cells
hdr_cells[0].text = 'Qty'
Run Code Online (Sandbox Code Playgroud)
我必须更改表格中一行“一列”的文字“数量”的字体大小,我该怎么做?
我正在使用 python docx 库,需要从文档中的表中读取数据。
虽然我可以使用以下代码读取数据,
document = Document(path_to_your_docx)
tables = document.tables
for table in tables:
for row in table.rows:
for cell in row.cells:
for paragraph in cell.paragraphs:
print(paragraph.text)
Run Code Online (Sandbox Code Playgroud)
我得到多个重复值,其中单元格中的内容跨越其合并的单元格,对于合并到其中的每个单元格一次。我不能简单地删除重复值,因为可能有多个未合并的单元格具有相同的值。我应该如何解决这个问题?
作为参考,我被指示从这个 github issue在这里提出问题。
谢谢你。
如何使用 Python 编辑 Word 文档中已存在的表格。假设在我的 word 文档中,我有一个只有 2 行的表格,我想在 Python 中添加更多行,我该怎么做?我已经尝试过使用docxlibrary,但我能做的最好的事情是创建一个表格并将其保存到 word 文档中。
我想编辑一个已经存在的表。谢谢!
python-docx ×10
python ×8
docx ×4
ms-word ×3
python-2.7 ×2
highlight ×1
pandas ×1
typeerror ×1