简体   繁体   English

使用 Python 在 Word 中的行尾查找多余的空格

[英]Finding the extra space at the end of a line in Word with Python

I am working on a project, but there are several things that I have had trouble finding documentation for.我正在做一个项目,但是有几件事我在查找文档时遇到了麻烦。 In Microsoft word, when you type out a sentence, if a word goes past the set margins it drops down to the next line (I know this is common knowledge, but it's relevant).在 Microsoft Word 中,当您输入一个句子时,如果一个单词超出设置的边距,它会下降到下一行(我知道这是常识,但它是相关的)。 Margins:边距:

|                               |
|Is this a good margin or is----|
|finding a better margin even---| 
|possible if we make minor------|
|adjustments.-------------------|

The space I've marked with the hyphens is what I am trying to find.我用连字符标记的空间是我想要找到的。 I could always make an estimate by finding the characters in a line, but I want the exact amount of blank space left over.我总是可以通过查找一行中的字符来进行估计,但我想要剩余的确切空格量。 So for line 1 with margins of x how much of the line is occupied by the section of characters?那么对于边距为 x 的第 1 行,字符部分占用了多少行?

Any insights would be helpful.任何见解都会有所帮助。

Assuming that your string's will have a '|'假设您的字符串将有一个 '|' at the end of them, the below code should work.最后,下面的代码应该可以工作。 Tell me if the |告诉我如果| will not always be there I'll remove my post.不会一直在那里 我会删除我的帖子。

x = "                               "[::-1]
spaces = 0
for i in x:
    if i == ' ':spaces += 1
    else:break

output output

31

Note: len(string) returns 31注意:len(string) 返回 31

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM