标签: python-docx

通过python读取Docx文件

有谁知道一个Python库来读取docx文件?

我有一个Word文档,我正在尝试从中读取数据。

python docx python-docx

4
推荐指数
0
解决办法
2万
查看次数

如何使用 python-docx 正确缩进?

缩进看起来非常简单,终端会打印回正确的缩进,但相同的缩进没有反映在我保存的 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 python-docx

4
推荐指数
1
解决办法
8852
查看次数

Python-Docx 将 HTML 插入 Docx

是否可以使用应用了样式的 python-docx 将 HTML 插入到文档中?我唯一需要做的就是斜体。

例如,如何插入"Today is <i>Saturday</i>."星期六实际上是用斜体插入的?

谢谢!

html python italics python-docx

4
推荐指数
2
解决办法
3903
查看次数

python-docx 如何将不同的样式应用于表格中的不同单元格

在 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 python-docx

4
推荐指数
1
解决办法
6954
查看次数

编辑文档Python-docx标题中的内容

我正在尝试查找并替换文档标题中文本框中的文本。但搜索了一段时间后,似乎无法通过 python-docx 访问标题或“浮动”文本框中的内容(我在这里阅读问题)

所以,这意味着我们必须直接在xml格式的文档上查找和替换。你知道该怎么做吗?

xml python-docx

4
推荐指数
1
解决办法
3776
查看次数

如何旋转表格单元格中的文本?

我正在尝试制作这样的表格:

具有垂直标题单元格的表格

如您所见,标题是垂直方向的。如何使用 python-docx 实现此目的?

PS 抱歉未翻译表格。

python-docx

4
推荐指数
1
解决办法
2037
查看次数

python-docx 获取标题文本

我想用 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 python-docx

4
推荐指数
1
解决办法
1万
查看次数

python-docx:在后面插入一个段落

在 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)

python python-docx

4
推荐指数
1
解决办法
2万
查看次数

Python-docx - 添加值后将单元格内容置于现有表格中居中

我有一个带有空表的 .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)

我感谢你的所有帮助。

docx python-docx

4
推荐指数
1
解决办法
3730
查看次数

使用 python-docx 在 Word 文档页眉中添加徽标

我希望每次运行代码时在 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-3.x python-docx

4
推荐指数
2
解决办法
7490
查看次数

标签 统计

python-docx ×10

python ×6

docx ×3

html ×1

italics ×1

python-3.x ×1

xml ×1