如何通过 python-docx 将图像插入文本背景(如重叠)?
我知道图像可以在其右侧或左侧放置文本。
但我想要像在任何文本上浮动图像之类的东西。就像背景图像一样。
有可能这样做吗?
谢谢。
我编写了一个使用 python-docx 来搜索 Word 文档(通过搜索运行)的参考编号和技术关键词的脚本,然后创建一个总结搜索结果的表格,该表格附加到 Word 文档的末尾。
有些文档有 100 多页,所以我想通过在搜索结果表中创建内部超链接来让用户更轻松,这样它会将您带到文档中检测到搜索结果的位置。
一旦找到参考运行,我不知道如何将其标记为书签或如何在结果表中创建指向该书签的超链接。
我能够使用本页中的代码创建外部网址的书签 通过使用 python-docx 在 MSWord 中添加超链接
我也尝试过创建书签,我找到了此页面: https: //github.com/python-openxml/python-docx/issues/109
标题与创建书签有关,但代码似乎在word中生成数字。
我觉得这两个解决方案可以放在一起,但我对 xml/word 文档没有足够的了解,无法做到这一点。
更新: 我发现一些代码可以将书签添加到Word文档中,现在需要的是一种使用Word文档中的链接链接到此的方法 https://github.com/python-openxml/python-docx/issues /403
*from docx import Document
def add_bookmark(paragraph, bookmark_text, bookmark_name):
run = paragraph.add_run()
tag = run._r # for reference the following also works: tag = document.element.xpath('//w:r')[-1]
start = docx.oxml.shared.OxmlElement('w:bookmarkStart')
start.set(docx.oxml.ns.qn('w:id'), '0')
start.set(docx.oxml.ns.qn('w:name'), bookmark_name)
tag.append(start)
text = docx.oxml.OxmlElement('w:r')
text.text = bookmark_text
tag.append(text)
end = docx.oxml.shared.OxmlElement('w:bookmarkEnd')
end.set(docx.oxml.ns.qn('w:id'), '0')
end.set(docx.oxml.ns.qn('w:name'), bookmark_name)
tag.append(end)
doc = Document("test_input_1.docx")
# add …Run Code Online (Sandbox Code Playgroud) 我正在尝试弄清楚如何在我的 django 应用程序中下载使用 python-docx 生成的 Word 文档(我仍在学习,这是我第一次使用文档);在ajax的帮助下,我将所需的所有信息发送到视图并调用使用该信息并返回文档的函数,然后我尝试将此文档作为响应发送,以便在“下载”的帮助下下载它”按钮(或显示网络浏览器下载对话框)在我提交数据的同一模板中,但这就是我陷入困境的地方。
发送此文档作为响应,以便在我提交数据的同一模板中的“下载”按钮(或显示网络浏览器下载对话框)的帮助下下载它,但这就是我陷入困境的地方。
到目前为止我所拥有的是:
1)在javascript中我发送的信息如下:
Run Code Online (Sandbox Code Playgroud)data = { categoria: cat, familia: fam, Gcas: gcas, FI: FI, FF: FF, Test: test, Grafica: grafica }, $.ajax({ type: 'post', headers: { "X-CSRFToken": csrftoken }, url: url, data: { json_data: JSON.stringify(data) }, success: function (response) { $('#instrucciones').hide(); //Hide a div with a message $('#btndesc').show(); //Show the button to download the file generated } }); return false; }
2)在我的Django视图中:
Run Code Online (Sandbox Code Playgroud)def Documento(request): if request.method == "GET": context={} context['form'] = catForm return …
我正在使用 python-docx 创建一个新文档,然后添加一个表(行=1,列=5)。然后我向五个单元格中的每一个添加一张图片。我的代码可以运行,但我从 docx 中看到的内容与我手动使用 Word 时看到的内容不同。
具体来说,如果我设置“显示格式标记”,然后查看 docx 生成的内容,则每个单元格的开头总是有一个硬回车(通过 add_paragraph 方法放置在那里)。当我手动使用 Word 时,没有硬性回报。
硬返回的结果是每张图片都比我想要的位置下降了一行。如果我使用 Word,图片就会出现在我期望的位置。
另外奇怪的是,在docx文档上我可以手动进去单击硬回车旁边,按一次下光标键,然后按一次退格键,硬回车被删除,图片移动到细胞的顶部。
所以我的问题是,有谁知道如何在执行 add_paragraph 方法时在表格单元格中获取图片而不需要硬返回?
任何帮助将不胜感激。
def paragraph_format_run(cell):
paragraph = cell.add_paragraph()
format = paragraph.paragraph_format
run = paragraph.add_run()
format.space_before = Pt(0)
format.space_after = Pt(0)
format.line_spacing = 1.0
format.alignment = WD_ALIGN_PARAGRAPH.CENTER
return paragraph, format, run
def main():
document = Document()
sections = document.sections
section = sections[0]
section.top_margin = Inches(1.0)
section.bottom_margin = Inches(1.0)
section.left_margin = Inches(0.75)
section.right_margin = Inches(0.75)
table = document.add_table(rows=1, cols=5)
table.allow_autofit = False
cells = …Run Code Online (Sandbox Code Playgroud) 我有一个表来填充要填充的表,我对 python-docx 很陌生。我尝试使用渲染来填充它,但它只作为输出提供给一台服务器:
for i in serverJson:
doc.render(i)
Run Code Online (Sandbox Code Playgroud)
其中 serverJson 是用户输入的服务器列表。例如:
for i in appserver:
server_1={"component":"Tomcat","comp_version":"7","server":i,
"app_port":"5000","db_sid":" ","db_port":"200"}
server_2={"component":"Apache","comp_version": "2.4","server":i,
"app_port":" ","db_sid":" ","db_port":"200"}
serverJson.append(server_1)
serverJson.append(server_2)
Run Code Online (Sandbox Code Playgroud)
我的问题是如何用用户输入的服务器数量填充链接中显示的表格?
我正在尝试启动单页烧瓶应用程序,允许用户下载word文档.我已经想出了如何使用python-docx制作/保存文档,但现在我需要在响应中提供文档.有任何想法吗?
这是我到目前为止所拥有的:
from flask import Flask, render_template
from docx import Document
from cStringIO import StringIO
@app.route('/')
def index():
document = Document()
document.add_heading("Sample Press Release", 0)
f = StringIO()
document.save(f)
length = f.tell()
f.seek(0)
return render_template('index.html')
Run Code Online (Sandbox Code Playgroud) 我正在尝试创建一个具有两行标题的表,该表的所有样式均使用简单的模板格式。两行标题是必需的,因为我在两个主要类别下具有相同的标题。看来,在Word中处理此问题的唯一方法是使文档在整个页面中重复头格式和进行格式化,这是将两行表嵌套到主内容表的头行中。
在Python-DocX中,始终使用单个空的段落元素创建表单元格。对于我的用例,我需要能够完全删除此空的段落元素,而不仅仅是用空字符串清除它。否则我的嵌套表上方会出现换行符,这会破坏我对单个表的幻想。
所以问题是如何删除空白段落?
如果您知道一种更好的方法来处理两行标头的实现...那也将是值得赞赏的信息。
我可以使用段落对象在表格单元格中选择字体大小,颜色,粗体等.但是,add_paragraph()似乎总是将一个前导\n插入到单元格中,这会混淆某些表格上的格式.
如果我只是使用该cell.text('')方法,它不会插入此换行符,但我无法控制文本属性.
有没有办法消除这个领先的换行线?
这是我的功能:
def add_table_cell(table, row, col, text, fontSize=8, r=0, g=0, b=0, width=-1):
cell = table.cell(row,col)
if (width!=-1):
cell.width = Inches(width)
para = cell.add_paragraph(style=None)
para.alignment = WD_ALIGN_PARAGRAPH.LEFT
run = para.add_run(text)
run.bold = False
run.font.size = Pt(fontSize)
run.font.color.type == MSO_COLOR_TYPE.RGB
run.font.color.rgb = RGBColor(r, g, b)
Run Code Online (Sandbox Code Playgroud) 我试图将数字和项目符号列表插入现有的Word文档,但它们显示为正常段落:
# Open up existing document
document = Document('existing_document.docx')
# Add style from example document
temp_doc = Document()
document.styles.add_style('List Number', temp_doc.styles['List Number'].type)
# Add bullet points from example document
p = document.add_paragraph()
p.style = 'List Number'
p.add_run('Item 1')
p = document.add_paragraph()
p.style = 'List Number'
p.add_run('Item 2')
# Save
document.save('new_doc_with_list.docx')
Run Code Online (Sandbox Code Playgroud)
这导致3段.没有缩进或编号.
我也尝试设置所有属性相同,然后完成运行,但这也不起作用:
document.styles['List Number'].base_style = temp_doc.styles['List Number'].base_style
document.styles['List Number'].next_paragraph_style = temp_doc.styles['List Number'].next_paragraph_style
document.styles['List Number'].paragraph_format.alignment = temp_doc.styles['List Number'].paragraph_format.alignment
document.styles['List Number'].paragraph_format.first_line_indent = temp_doc.styles['List Number'].paragraph_format.first_line_indent
document.styles['List Number'].paragraph_format.keep_together = temp_doc.styles['List Number'].paragraph_format.keep_together
document.styles['List Number'].paragraph_format.keep_with_next = …Run Code Online (Sandbox Code Playgroud) 我想逐段阅读docx文档,如果有图片(InlineShape),请使用其周围的文字进行处理。函数Document.inline_shapes将提供文档中所有内联形状的列表。但是我想得到一个,如果存在的话,它恰好出现在当前段落中...
代码示例:
from docx import Document
doc = Document("test.docx")
blip = doc.inline_shapes[0]._inline.graphic.graphicData.pic.blipFill.blip
rID = blip.embed
document_part = doc.part
image_part = document_part.related_parts[rID]
fr = open("test.png", "wb")
fr.write(image_part._blob)
fr.close()
Run Code Online (Sandbox Code Playgroud)
(这就是我要保存这些图片的方式)
python-docx ×10
python ×8
docx ×3
image ×2
ms-word ×2
python-3.x ×2
django ×1
flask ×1
httpresponse ×1
javascript ×1
jinja2 ×1
overlapping ×1
overlay ×1