【问题标题】:Why some words in the dictionary are not replaced?为什么字典中的某些单词没有被替换?
【发布时间】:2022-06-13 21:55:56
【问题描述】:
translation = {'                                             Cloud AI ': 'ਕਲਾਊਡ AI',
 ' Architecture': 'ਆਰਕੀਟੈਕਚਰ',
 ' Conclusion': 'ਸਿੱਟਾ',
 ' Motivation / Entity Extraction': 'ਪ੍ਰੇਰਣਾ / ਹਸਤੀ ਕੱਢਣ',
 ' Recurrent Deep Neural Networks': 'ਆਵਰਤੀ ਡੂੰਘੇ ਨਿਊਰਲ ਨੈੱਟਵਰਕ',
 ' Results': 'ਨਤੀਜੇ',
 ' Word Embeddings': 'ਸ਼ਬਦ ਏਮਬੈਡਿੰਗਸ',
 'Agenda': 'ਏਜੰਡਾ',
 'Also known as Named-entity recognition (NER), entity chunking and entity identification': 'ਨਾਮ-ਹਸਤੀ ਮਾਨਤਾ (NER), ਇਕਾਈ ਚੰਕਿੰਗ ਅਤੇ ਇਕਾਈ ਪਛਾਣ ਵਜੋਂ ਵੀ ਜਾਣਿਆ ਜਾਂਦਾ ਹੈ',
 'Biomedical Entity Extraction': 'ਬਾਇਓਮੈਡੀਕਲ ਇਕਾਈ ਐਕਸਟਰੈਕਸ਼ਨ',
 'Biomedical named entity recognition': 'ਬਾਇਓਮੈਡੀਕਲ ਨਾਮੀ ਇਕਾਈ ਦੀ ਮਾਨਤਾ',
 'Critical step for complex biomedical NLP tasks:': 'ਗੁੰਝਲਦਾਰ ਬਾਇਓਮੈਡੀਕਲ NLP ਕਾਰਜਾਂ ਲਈ ਮਹੱਤਵਪੂਰਨ ਕਦਮ:',
 'Custom Entity Extraction': 'ਕਸਟਮ ਇਕਾਈ ਐਕਸਟਰੈਕਸ਼ਨ',
 'Custom models': 'ਕਸਟਮ ਮਾਡਲ'}

Slide of ppt. If you could see the first word "Custom" is not replaced even though it is present in the dictionary translation

我想知道为什么某些单词会出现这种情况。

换词代码

prs = Presentation('/content/drive/MyDrive/presentation2.pptx')


# To get shapes in your slides

slides = [slide for slide in prs.slides]
shapes = []
for slide in slides:
    for shape in slide.shapes:
        shapes.append(shape)


def replace_text(replacements: dict, shapes: List[str]):
    """Takes dict of {match: replacement, ... } and replaces all matches.
    Currently not implemented for charts or graphics.
    """
    for shape in shapes:
        for match, replacement in replacements.items():
            if shape.has_text_frame:
                if (shape.text.find(match)) != -1:
                    text_frame = shape.text_frame
                    for paragraph in text_frame.paragraphs:
                        for run in paragraph.runs:
                            cur_text = run.text
                            new_text = cur_text.replace(match, replacement)
                            run.text = new_text

            if shape.has_table:
                for row in shape.table.rows:
                    for cell in row.cells:
                        if match in cell.text:
                            new_text = cell.text.replace(match, replacement)
                            cell.text = new_text

replace_text(translation, shapes) 

prs.save('output5.pptx')

run.text 输出

Custom Entity Extraction - Custom इकाई निष्कर्षण

【问题讨论】:

  • 先打印每个run.text,看看你会得到什么
  • 问题可能不在于代码的 powerpoint 部分。了解如何创建minimal reproducible example 并编辑问题。
  • replace 只替换第一个匹配项。
  • @PeterWood 我已经发布了 run.text 的结果
  • @PeterWood 不正确。 replace 默认替换所有。只有第一个 n 匹配有一个可选参数。

标签: python python-3.x dictionary python-pptx


猜你喜欢
  • 2021-12-10
  • 1970-01-01
  • 2020-12-11
  • 1970-01-01
  • 2022-01-07
  • 2020-01-17
  • 1970-01-01
  • 2013-09-10
  • 1970-01-01
相关资源
最近更新 更多