text.replace in python-docx deletes all footnotes in document regardless of whether they contain replaced text - docx

I am trying to write a Python script that replaces certain target words in .docx files with predefined text. My script replaces the target words but, if it finds a match anywhere in the document, the script deletes all of the footnotes. This is true regardless of whether there are any matches in the footnotes. If the target word does not appear in the document at all, the footnotes are unaffected.
I am at a complete loss to understand this behavior. This issue can be replicated with just six lines of code:
from docx import Document
doc = Document("./Test Input.docx")
for p in doc.paragraphs:
if p.text.find("target") >= 0:
p.text = p.text.replace("target", "replacement")
doc.save("./Test Output.docx")
I know I am pushing the boundaries of what python-docx can handle, but I am hoping somebody can help me figure out what is going on here.

Related

How to select a random word from a txt file in python version 3.4?

**
I have a txt file called 'All_Words' and it consist of 2000 words and i'm making a hangman so i needed to choose a random word i've already thought of picking a random number from 0 to 2000 and read the line of the number to chose but i don't know how to do that, also some background info:
i am in 8th grade and i like coding im trying to get better so i'm trying to get what people suggest and try to figure out what every part does and the reserved words such as 'global' for example
also i have also tried to just shuffle the txt file because i already got it to print the first word so if im a able to shuffle the txt file then it would print a different word an i could create an if statement saying if the word chosen was already chooses then it would shuffle it again and pick he first word again, also i got this idea of the shuffle the txt file from my dad but he only did something called 'dos' or something like that he said he did it before it was even called coding so i don't even know if it world word in python, and i've asked my coding teacher and he said he dont know how you would do that because he is use to java and javascript
this is what i have so far and also i would like it to only pick one word instead of every word in order:**
import random
with open("All_Words.txt") as file:
for line in file:
print(line)
break
Assuming each word is on a new line in file, you can read the text file into a list and use random.choice() to pick a random element in the list. Then you remove the word from the list so you don't pick it again.
import random
file = open("All_Words.txt", "r")
words = file.read()
listOfWords = words.split("\n")
randWord = random.choice(listOfWords)
print(randWord)
listOfWords.remove(randWord)
newUnqiueRandWord = random.choice(listOfWords)
print(newUnqiueRandWord)

Create new emphasis command R Markdown

In R Markdown, to make a text bold, we just need to do:
**code**
The the word code shows in bold.
I was wondering if there is a way to create a new command, let's say:
***code***
That would make the text highlighted?
Thanks!
It is not easily possible to create new markup, but one can change the way existing markup commands are rendered. Text enclosed by three stars is interpreted as emphasized strong emphasis. So one has to change that interpretation and change it to something else. One way to do so is via pandoc Lua filters. We just have to match on pandoc's internal representation of emphasized strong text and convert it to whatever we want:
function Strong (strong)
-- if this contains only one element, and if that element
-- is emphasized text, convert it to highlighted text.
local element = #strong.content == 1 and strong.content[1]
if element and element.t == 'Emph' then
table.insert(element.content, 1, pandoc.RawInline('html', '<mark>'))
table.insert(element.content, pandoc.RawInline('html', '</mark>'))
return element.content
end
end
The above works for HTML output. One would have to define what "highlighted text" means for each targeted format.
See this and this question for other approaches to the problem, and for details of how to use the filter with R Markdown.

Lyx. Space between letters of some words in program listing

When I create a pdf document that I'm writing in Lyx, there are spaces between letters in some words when I use code programs to insert some pieces of programming.
In the Lyx program listing configuration I added the option "showstringspaces = false" but I do not get anything.
Can you tell me how I can remove these annoying spaces so that all the letters of each word in the code lists appear together?
I get ---> fmt. P r i n t
I expect ---> fmt.Print
I answer to myself. Putting the option columns = fullflexible or columns = flexible in the configuration of the code lists is solved

How to find the type of a list using Novacode DocX?

I'm parsing a .docx document using Novacode DocX, and so far I can detect if a paragraph is part of a list with Paragraph.IsListItem. However, I want to distinguish between lists numbered with Arabic numbers (1. 2. 3.), Roman numbers (I. II. III.) or letters (a. b. c.).
I can't find any property in Paragraph that gives me this information. Is there any way to get it?

Implementing syntax highlighting for markdown titles in PySide/PyQt

I am trying to implement a syntax highlighter for markdown for my project in PySide. The current code covers the basic, with bold, italic, code blocks, and some custom tags. Below is an extract of the relevant part of the current code.
What is blocking me right now is how to implement the highlighting for titles (underlined with ===, for the main title, or --- for sub-titles). The method that is used by Qt/PySide to highlight the text is highlightBlock, which processes only one line at a time.
class MySyntaxHighlighter(QtGui.QSyntaxHighlighter):
def highlightBlock(self, text):
# do something with this line of text
self.setCurrentBlockState(0)
startIndex = 0
if self.previousBlockState() != 1:
startIndex = self.blockStartExpression.indexIn(text)
while startIndex >= 0:
endIndex = self.blockEndExpression.indexIn(
text, startIndex)
...
There is a way to recover the previousBlockState, which is useful when a block has a defined start (for instance, the ~~~ syntax at the beginning of a code-block). Unfortunately, there is nothing that defines the start of a title, except for the underlining with === or --- that take place on the next line. All the examples I found only handle cases where there is a defined start of the expression, and so that the previousBlockState gives you an information (as in the example above).
The question is then: is there a way to recover the text of the next line, inside the highlightBlock? To perform a look-ahead, in some sense.
I though about recovering the document currently being worked on, and find the current block in the document, then find the next line and make the regular expression check on this. This would however break if there is a line in the document that has the exact same wording as the title. Plus, it would become quite slow to systematically do this for all lines in the document. Thanks in advance for any suggestion.
If self.currentBlock() gives you the block being highlighted, then:
self.currentBlock().next().text()
should give you the text of the following block.

Resources