小编Won*_*ket的帖子

使用 python-docx 突出显示文本

我想突出显示 docx 中的文本并将其保存到另一个文件。这是我的代码

from docx import Document

def highlight_text(filename):

    doc = Document(filename)
    for p in doc.paragraphs:
        if 'vehicle' in p.text:
            inline = p.runs
            # print(inline)
            # Loop added to work with runs (strings with same style)
            for i in range(len(inline)):
                # print((inline[i].text).encode('ascii'))
                if 'vehicle' in inline[i].text:
                    x=inline[i].text.split('vehicle')
                    inline[i].clear()
                    for j in range(len(x)-1):
                        inline[i].add_text(x[j])
                        y=inline[i].add_text('vehicle')
                        y.highlight_color='YELLOW'
            # print (p.text)

    doc.save('t2.docx')
    return 1
if __name__ == '__main__':

    highlight_text('t1.docx')
Run Code Online (Sandbox Code Playgroud)

这个词没有突出显示我做错了什么。

python python-docx

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

为什么当x**y没有时math.pow抛出OverflowError?

根据文档, math.pow功能如下:

def pow(x, y):
    """
    pow(x, y)

    Return x**y (x to the power of y).
    """
    pass
Run Code Online (Sandbox Code Playgroud)

我决定乱用大数字,但是当我运行这个时我收到一个错误:

import math
calc = math.pow(1000, 103)
print(calc)

Traceback (most recent call last):
  File "test.py", line 3, in <module>
    print(math.pow(1000, 103))
OverflowError: math range error
Run Code Online (Sandbox Code Playgroud)

但是,以下没有错误:

calc = 1000 ** 103
print(calc)
>> (very large number)
Run Code Online (Sandbox Code Playgroud)

如果math.pow假设起作用x**y,为什么它抛出一个OverflowError而明确做x**y不?

python math

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

如何在python-docx中同时应用粗体和斜体?

我正在写一本字典.我正在使用python-docx将其放入MS Word中.我可以很容易地使它变得粗体或斜体,但似乎无法弄清楚如何做到这两点.这是基础知识:

import docx

word = 'Dictionary'

doc = docx.Document()
p = doc.add_paragraph()
p.add_run(word).bold = True

doc.save('test.docx')
Run Code Online (Sandbox Code Playgroud)

我试过p.add_run(word).bold.italic = True,但收到'NoneType'错误,我明白了.

我也在add_run之前和之后尝试了p.bold = True和p.italic = True,但是一起丢失了格式.

Word的查找/替换是一个简单的解决方案,但如果可以,我宁愿在代码中执行此操作.

python docx python-docx

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

python函数输出无

import random
def getAnswer (answerNumber):
    if getAnswer == 1:
        return 'It is certain'
    elif getAnswer == 2:
        return 'It is decidedly so'  
    elif getAnswer == 3:
        return 'Yes' 
    elif getAnswer == 4:
        return "Reply hazy try again"
    elif getAnswer == 5:
        return 'Ask again later'
    elif getAnswer == 6:
        return "Concentrate and ask again"
    elif getAnswer == 7:
        return 'My reply is no'
    elif getAnswer == 8:
        return 'not so good'
    elif getAnswer == 9:
        return 'doubtful'

r = random.randint(1 …
Run Code Online (Sandbox Code Playgroud)

python python-3.x

0
推荐指数
1
解决办法
76
查看次数

函数不返回值

A=[3,4,2,1,2,2,2,3,2,3,2,3,5,6,2]

def Sort(x):

    count=0
    for t in range(len(x)-1):
        if x[t]>x[t+1]:
            count+=1
            m,n=x[t+1],x[t]
            x[t+1],x[t]=n,m
    if count==0:
        return x
    else:
        Sort(x)

print Sort(A)
Run Code Online (Sandbox Code Playgroud)

python python-2.7

-2
推荐指数
1
解决办法
45
查看次数

标签 统计

python ×5

python-docx ×2

docx ×1

math ×1

python-2.7 ×1

python-3.x ×1