首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Python:查找Word doc中的所有占位符数字并将其替换为随机数。

Python:查找Word doc中的所有占位符数字并将其替换为随机数。
EN

Stack Overflow用户
提问于 2021-12-06 22:22:03
回答 2查看 1.1K关注 0票数 0

我在查找和替换Word文件段落中的几个占位符时遇到了困难。这是一个游戏手册,所以我试着为占位符,在起草书时使用的子随机条目数字。

所有占位符都以"#“开头(例如#1-5,#22-1,等等)。设置数字,就像第一个条目(总是"1")一样,没有"#“前缀。占位符条目通过在元组中拉链作为元组与随机对应项配对,以供参考。

这一切都是伟大的标题,因为它是一个直线一对一的段落互换,有序。问题是,当我遍历常规段落时(仅次于最后一段代码)。它似乎只取代了前八个数字,然后停止了。我试过设置一个循环,但似乎没什么用。不知道我错过了什么。代码如下。

编辑:下面是如何设置两个列表和引用元组。在这个测试中,只设置了第一个条目,没有在段落中引用它.所有其他的都是随机的,并在段落中替换。

entryWorking:'#1-1','#1-2','#1-3','#1-4','#1-5','#1-6','#1-7','#1-8','#2','#2-1','#2-2','#2-3','#2-4','#2-5','#2-6','#2-7',“#16”、“#17”、“#3”、“#3-1”、“#3-2”、“#3-3”、“#3-4”、“#3-5”、“#3-6”、“#3-8”、“#3-9”

entryNumbers:'2‘、'20’、'12‘、'27’、'23‘、'4’、'11‘、'16’、'26‘、'7’、'25‘、'5’、'3‘、'15’、'17‘、'6’、'18‘、'22’、'10‘、'21’、'19‘、'13’、'28‘、'8’、'14‘、'9',“24”

参考文献:(‘1-1’,'2'),(‘1-2’,'20'),(‘1-3’,'12'),(‘1-4’,'27'),('#1-5','23'),(‘1-6’,'4'),(‘1-7’,'11'),(‘1-8’,'16'),('#2',“26”、(“2-1”、“7”)、(“2-2”、“25”)、(“2-3”、“5”)、(“2-4”、“3”)、(“2-5”、“15”)、(“2-6”、“17”)、(‘2-7’、'6')、(‘16’、'18')、(‘17’,“22”、(“3”、“10”)、(“3-1”、“21”)、(“3-2”、“19”)、(“3-3”、“13”)、(“3-4”、“28”)、(“3-5”、“8”)、(“3-6”、“14”)、(“3-8”、“9”),(“#3-9”,“24”)

谢谢你的协助。

代码语言:javascript
复制
import sys, os, random
from docx import *

entryWorking = [] # The placeholder entries created for the draft gamebook


# Identify all paragraphs with a specific heading style (e.g. 'Heading 2')
def iter_headings( paragraphs, heading ) :
    for paragraph in paragraphs :
        if paragraph.style.name.startswith( heading ) :
            yield paragraph


# Open the .docx file
document = Document( 'TestFile.docx' )


# Search document for unique placeholder entries (must have a unique heading style)
for heading in iter_headings( document.paragraphs, 'Heading 2' ) :
    entryWorking.append( heading.text )


# Create list of randomized gamebook entry numbers
entryNumbers = [ i for i in range( len ( entryWorking ) + 1 ) ]

# Remove unnecessary entry zero (extra added above to compensate)
entryNumbers.remove( 0 )

# Convert to strings
entryNumbers = [ str( x ) for x in entryNumbers ]


# Identify pre-set entries (such as Entry 1), and remove from both lists
# This avoids pre-set numbers being replaced (i.e. they remain as is in the .docx)
# Pre-set entry numbers must _not_ have the "#" prefix in the .docx
for string in entryWorking :
    if string[ 0 ] != '#' :
        entryWorking.remove( string )
        if string in entryNumbers :
            entryNumbers.remove( string )

# Shuffle new entry numbers
random.shuffle( entryNumbers )


# Create tuple list of placeholder entries paired with random entry
reference = tuple( zip( entryWorking, entryNumbers ) )


# Replace placeholder headings with assigned randomized entry
for heading in iter_headings( document.paragraphs, 'Heading 2' ) :
    for entry in reference :
        if heading.text == entry[ 0 ] :
            heading.text = entry[ 1 ]


# Search through paragraphs for placeholders and replace with randomized entry
for paragraph in document.paragraphs :
    for run in paragraph.runs :
        for entry in reference :
            if run.text == entry[ 0 ] :
                run.text = entry [ 1 ]

                        
# Save the new document with final entries
document.save('Output.docx')
EN

Stack Overflow用户

回答已采纳

发布于 2021-12-07 03:23:24

在Word中,在文本中任意位置运行中断:

您可能对这个答案中的链接感兴趣,这些链接演示了在一般情况下完成这类工作所需的(令人惊讶的复杂)工作:

如何使用python-docx替换Word文档中的文本并保存

有几个段落级函数做得很好,可以在python的GitHub站点上找到。 这个会用替换的str替换正则匹配。。替换字符串的格式将与匹配字符串的第一个字符相同。 这个隔离运行可以将一些格式应用于该单词或短语,例如突出显示文本中出现的"foobar“,或者使其粗体或以较大字体显示。

幸运的是,它通常是可复制的,具有良好的效果:)

票数 0
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70252679

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档