有谁知道一个Python库来读取docx文件?
我有一个Word文档,我正在尝试从中读取数据。
缩进看起来非常简单,终端会打印回正确的缩进,但相同的缩进没有反映在我保存的 Word docx 中。
我在这里做错了什么吗?
from docx import Document
from docx.shared import Inches
worddoc = Document()
paragraph = worddoc.add_paragraph('Left Indent Test')
paragraph.left_indent = Inches(.25)
print(paragraph.left_indent.inches)
worddoc.save('left_indent.docx')
Run Code Online (Sandbox Code Playgroud) 是否可以使用应用了样式的 python-docx 将 HTML 插入到文档中?我唯一需要做的就是斜体。
例如,如何插入"Today is <i>Saturday</i>."星期六实际上是用斜体插入的?
谢谢!
在 python-docx 中处理表格。我试图对表格的第一行与其余行应用不同的样式。这将是标题行。这是表格:
table2 = doc.add_table(rows=10, cols=3)
heading_cells_table2 = table2.rows[0].cells
heading_cells_table2[0].text = 'Header 1'
heading_cells_table2[1].text = 'Header 2'
heading_cells_table2[2].text = 'Header 3'
Run Code Online (Sandbox Code Playgroud)
我希望我能做这样的事情:
table2.rows[0].style = table_heading_style
Run Code Online (Sandbox Code Playgroud)
我尝试了 .add_run()、.cells、.font、.text 等的许多其他变体和组合,但没有成功。
是否可以更改表格中单个单元格/行/列的格式?如果是这样,是如何做到的?
谢谢您的帮助。
编辑
对于这个问题的答案的具体示例,以下是我最终如何将文本集中在表格中 3 列中的 2 列中,这要感谢 scanny 的帮助。
for column in itertools.islice(table2.columns, 1, 3):
for cell in column.cells:
for paragraph in cell.paragraphs:
paragraph.paragraph_format.alignment = WD_ALIGN_PARAGRAPH.CENTER
Run Code Online (Sandbox Code Playgroud) 我正在尝试查找并替换文档标题中文本框中的文本。但搜索了一段时间后,似乎无法通过 python-docx 访问标题或“浮动”文本框中的内容(我在这里阅读问题)
所以,这意味着我们必须直接在xml格式的文档上查找和替换。你知道该怎么做吗?
我想用 Python 从 docx 文件中读取标题文本。我正在使用 python-docx 模块。
如果此功能已经实现,有人可以帮助我做到这一点吗?
我尝试这样做,但没有成功。
from docx import Document
document = Document(path)
section = document.sections[0]
print(section.text)
Error:
<class 'AttributeError'>'Section' object has no attribute 'text'
Run Code Online (Sandbox Code Playgroud)
和:
from docx import Document
document = Document(path)
header = document.sections[0].header
print(header.text)
Error:
<class 'AttributeError'>'Section' object has no attribute 'header'
Run Code Online (Sandbox Code Playgroud) 在 python-docx 中,该paragraph对象有一个方法insert_paragraph_before允许在其自身之前插入文本:
p.insert_paragraph_before("This is a text")
Run Code Online (Sandbox Code Playgroud)
没有insert_paragraph_after方法,但我认为段落对象充分了解自己,可以确定列表中的下一个段落。不幸的是,python-docx AST 的内部工作原理有点复杂(并且没有真正记录)。
我想知道如何使用以下规格对函数进行编程?
def insert_paragraph_after(para, text):
Run Code Online (Sandbox Code Playgroud) 我有一个带有空表的 .docx 模板,我在其中添加值:
def manipulate_table():
table = doc.tables[0]
table.cell(0, 0).text = 'A'
table.cell(0, 1).text = 'B'
table.cell(0, 2).text = 'C'
table.cell(0, 3).text = 'D'
Run Code Online (Sandbox Code Playgroud)
添加这些值后,表属性“居中”消失,这是标准行为。
如何循环遍历表格并再次居中所有值?我已经用谷歌搜索过,但没有发现任何有用的信息。例如: 不起作用:
for cell in ....????:
tc = cell._tc
tcPr = tc.get_or_add_tcPr()
tcVAlign = OxmlElement('w:vAlign')
tcVAlign.set(qn('w:val'), "center")
tcPr.append(tcVAlign)
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 中可能吗?还是我应该尝试其他库来做到这一点?如果可能的话请告诉我怎么做