python3's "pdfkit"-import keeps destroying german umlaute - python

Hey people and codingfriends.
Im currently writing a little tool in python3 which i want to write some userinput from a .txt-file into a pdf-file. For this i am using "pdfkit". Im first writing my input into a 2nd. txt file, because i need the output in a special form and with "pdfkit.from_string(source, destination.pdf)" it doesnt seem to work properly. So im Writing it first into a 2nd txt file how i already said and then converting it with "pdfkit.from_file("source.txt", destination.pdf)". I already sorted out the problem with the different unicodestuff but somehow the "pdfcreator" keeps deleting/changing all "umlaute". I guess i can figure out why it keeps doing that, but i cant cant find a solution due its a 3rd party import. The createt txt file btw is in exactly the form i need it, just the pdf stuff is not working.
In example:
Does anyone have a solution for me?
For me would also work:
writing from python into a different txt format which is easy to convert into pdf (with umlaute!)
OR
using a 3rd party-software which i am calling from python to convert my txt file
So far i got this code(including some readLines methods which are also working pretty well - i guess):
def dok2pdf(fileName):
dokument=open(fileName, "r", encoding="latin-1").read()
for line in dokument:
if line==" ":
dokument=dokument.replace(line, " ")
if line==";":
dokument=dokument.replace(line, "\n")
file = open("tryhard.txt","w")
file.write(dokument)
file.close()
pdfkit.from_file("tryhard.txt", 'tryhard.pdf')
thanks a lot.

Related

Python - getline() method not printing "newlines"

I am relatively new to python and I am making a small game that involves importing each line of the text from a .txt file, so that it can be printed to the user. To do this, I'm using linecache.getline() to get the specific line of the file that I want while not having the whole file stored as a list. However, if I use "\n" to create a new line, then linecache.getline() automatically inserts another backslash to "cancel" it.
For example, if in the text file I write
\nHello,
linecache.getline() will store it in the variable as
\\nHello
which prints as
\nHello.
Is there any way to stop this? I can post my specific code if required.
Any help with file manipulation will be appreciated since I am very new to it and thank you for looking at my question.

Python - writing string to HTML file causes computational issues

I appreciate that this may be an issue with my computer/software, but I want to double check that my code isn't causing the problem before ruling it out.
I have written a fairly simple program. I have a short list of strings read in from a text file, then with a different text file open, I iterate over each word in the second text file, checking if the first two letters of the word are contained in the first list of strings.
Then, if that condition is fulfilled, I use string interpolation to insert that word into a string of HTML code. Finally, I append that string to an existing empty .html. When finished iterating through, I close the html file.
with open("strings.txt", "r") as f:
strings = f.read().splitlines()
urlfile = open("links.html", "a")
with open("words.txt", "r") as f:
text = f.read().splitlines()
for word in text:
if word[:2] in strings:
html = '<a href="[URL]/{}">'.format(word)
urlfile.write(html)
urlfile.close()
so far there doesn't actually seem to be any issues with my code doing what I want - I am generating the right html code and if I print it to console it does so quickly. It is being appended to the html file.
The problem I have is that something I am doing must be computationally expensive or problematic, because Notepad++ freezes every time I try to check links.html for the results. I have managed to see that it looks correct, but Notepad++ then becomes unusable, and my computer is clearly straining. The only solution I have is to close anything related to the html file.
None of the lists used are long and all the operations should in theory be quite simple, so I feel as though I must be doing something wrong. Am I writing to files in an unsafe way? Am I doing something wildly expensive that I'm just missing? I am using Notepad++ v7.9.5, Python 3, and Anaconda prompt.
EDIT: I am now able to access the html file on my browser and on Notepad++ without issue. I think the source of the problem was some laptop software updating in the background without me noticing. I'll check that first next time!

Find lines which contain a specific word

First of all I have done a great research and tried many approaches to look for solution but maybe I am doing it wrong and was not able to find a solution.
My data:
https://knsim.com/test.txt
The problem is that I want to fill all the occurrence of 'Idea_Print' in a new file (not just the name 'Idea_Print' but the complete line of log.
I have tried many approaches but had not success.
My recent code:
file = open(filename, 'r')
for line in file:
if 'Idea_Print' in line:
print(line)
But this didn't work for me. I even tried using re but no success.
Thanks for help.
I tried running your code with my setup and it worked fine. Most likely, you are using the wrong filename in open().
In file = open(filename, 'r'), make sure that filename is the exact same name as your saved data, including extension. Ensure that the file you are trying to access is in the same folder as your python file. If it is not in the same folder, try including the full path to the file.
Also, at the end of your program make sure you call file.close() to allow other programs to access it correctly.
Another thing that could be causing problems is that your variable is called file. This is a built-in name in Python, so you probably don't want to overwrite it. Try changing it to something like data_file.
Edit: Looking at that file it appears that there is a space between every character. That means you will need to use 'E p i s o d e _ 2 0' instead of 'Episode_20'. However, it appears that even that does not appear in the file. Maybe you should double-check that it is the text that you are looking for.

How can I make Python using csv read the first word in a line rather than the first character?

I've looked around for solutions to this here, and though they seemed to fit my needs they ended up not working for me. I'm not particularly sure why. I'll try and specify what the issue is and hopefully you'll be able to specify why this was different.
So, I've just been trying to make a simple username and password log-in system using a Tkinter GUI. I've tried making this work by putting the usernames and passwords in an external CSV file, and then having my program, once both users for my game that's already functioning have attempted to log-in, read line-by-line my csv file (called "Accepted Users for Card Game.csv"), which is in the same folder as the program so no further directory is necessary. I've tried making it so that the user puts their username and password into different Tkinter entries, and then presses a button beneath it. The program then checks if, for example, line[0] == PlayerOneUsername and line[1] == PlayerOnePassword, however every time I've tried this, it didn't work. (My delimiter is ',', for anybody wondering).
After making it print what it was reading, I've discovered that it always returns the first character of the line rather than the first word, so of course it keeps on being incorrect. I've searched for an answer, some of which I was already trying, however every time I've tried implementing it, the same issue seems to have been occurring. (EG line.split() hasn't fixed my problem).
This is the output on comparing the line and line[0] next to each other. (Ignore the colours and the title afterwards, those are to do with the game).
Here is the portion of my player two evaluation that triggers when you press the button (it's more or less identical to my player one evaluation):
Success["text"] = ""
if not PasswordLoginInputTwo.get() or not UsernameLoginInputTwo.get():
Success["text"] = "One or more of your password/username entries are empty. Please try again."
else:
csvfile = open ("Accepted Users for Card Game.csv", "r")
Csvreader = csv.reader(csvfile, delimiter = ',')
Success["text"] = "Player Two password/username combination has been read."
for line in csvfile:
print(line[0])
if line:
print(line)
print(line[0])
if line.split(None, 1)[0] == UsernameLoginInputTwo.get() and line.split(None, 1)[1] == PasswordLoginInputTwo.get():
PlayerTwoSuccess = True
PlayerTwoUsername = UsernameLoginInputTwo.get()
PlayerTwoColour = line[2]
PlayerTwoAntithesis = line[3]
PlayerTwoVictorSound = "Victory - "+line[4]+".wav"
PlayerTwoAttempts = PlayerTwoAttempts + 1
The external csv file is just arranged in a simple line format, like this:
PlayerOne,Password1,Purple,Yellow,Donkey Kong
PlayerTwo,Password2,Red,Green,Final Fantasy
PlayerThree,Password3,Pink,Purple,Worms
PlayerFour,Password4,Black,White,Team Fortress 2
I know the answer is probably rather obvious but I'm quite annoyed that I can't seem to find the answer! I'm using Python 2.7 by the way, though I don't intend to carry on using it. (As you can probably tell, I'm extremely new to Python, so please explain any solution like I'm extremely dumb - though you probably already got that impression from my code and the overall problem haha). Thanks!
So basically, to repeat, I am under the impression that line[0] should be the first 'word' in my 'line', in this case being for example "PlayerOne". Instead, it is giving me the first character, in this case, 'P'. How can I rectify this issue? What is causing this? How can I make it so it's the first word rather than the first character? Thank you!
You are iterating over:
for line in csvfile:
Which will return a string representing the first line. You need to loop over the reader object:
for line in Csvreader:
Then, line will be a tuple of the strings split on the delimiter.

Write strings to another file

The Problem - Update:
I could get the script to print out but had a hard time trying to figure out a way to put the stdout into a file instead of on a screen. the below script worked on printing results to the screen. I posted the solution right after this code, scroll to the [ solution ] at the bottom.
First post:
I'm using Python 2.7.3. I am trying to extract the last words of a text file after the colon (:) and write them into another txt file. So far I am able to print the results on the screen and it works perfectly, but when I try to write the results to a new file it gives me str has no attribute write/writeline. Here it the code snippet:
# the txt file I'm trying to extract last words from and write strings into a file
#Hello:there:buddy
#How:areyou:doing
#I:amFine:thanks
#thats:good:I:guess
x = raw_input("Enter the full path + file name + file extension you wish to use: ")
def ripple(x):
with open(x) as file:
for line in file:
for word in line.split():
if ':' in word:
try:
print word.split(':')[-1]
except (IndexError):
pass
ripple(x)
The code above works perfectly when printing to the screen. However I have spent hours reading Python's documentation and can't seem to find a way to have the results written to a file. I know how to open a file and write to it with writeline, readline, etc, but it doesn't seem to work with strings.
Any suggestions on how to achieve this?
PS: I didn't add the code that caused the write error, because I figured this would be easier to look at.
End of First Post
The Solution - Update:
Managed to get python to extract and save it into another file with the code below.
The Code:
inputFile = open ('c:/folder/Thefile.txt', 'r')
outputFile = open ('c:/folder/ExtractedFile.txt', 'w')
tempStore = outputFile
for line in inputFile:
for word in line.split():
if ':' in word:
splitting = word.split(':')[-1]
tempStore.writelines(splitting +'\n')
print splitting
inputFile.close()
outputFile.close()
Update:
checkout droogans code over mine, it was more efficient.
Try this:
with open('workfile', 'w') as f:
f.write(word.split(':')[-1] + '\n')
If you really want to use the print method, you can:
from __future__ import print_function
print("hi there", file=f)
according to Correct way to write line to file in Python. You should add the __future__ import if you are using python 2, if you are using python 3 it's already there.
I think your question is good, and when you're done, you should head over to code review and get your code looked at for other things I've noticed:
# the txt file I'm trying to extract last words from and write strings into a file
#Hello:there:buddy
#How:areyou:doing
#I:amFine:thanks
#thats:good:I:guess
First off, thanks for putting example file contents at the top of your question.
x = raw_input("Enter the full path + file name + file extension you wish to use: ")
I don't think this part is neccessary. You can just create a better parameter for ripple than x. I think file_loc is a pretty standard one.
def ripple(x):
with open(x) as file:
With open, you are able to mark the operation happening to the file. I also like to name my file object according to its job. In other words, with open(file_loc, 'r') as r: reminds me that r.foo is going to be my file that is being read from.
for line in file:
for word in line.split():
if ':' in word:
First off, your for word in line.split() statement does nothing but put the "Hello:there:buddy" string into a list: ["Hello:there:buddy"]. A better idea would be to pass split an argument, which does more or less what you're trying to do here. For example, "Hello:there:buddy".split(":") would output ['Hello', 'there', 'buddy'], making your search for colons an accomplished task.
try:
print word.split(':')[-1]
except (IndexError):
pass
Another advantage is that you won't need to check for an IndexError, since you'll have, at least, an empty string, which when split, comes back as an empty string. In other words, it'll write nothing for that line.
ripple(x)
For ripple(x), you would instead call ripple('/home/user/sometext.txt').
So, try looking over this, and explore code review. There's a guy named Winston who does really awesome work with Python and self-described newbies. I always pick up new tricks from that guy.
Here is my take on it, re-written out:
import os #for renaming the output file
def ripple(file_loc='/typical/location/while/developing.txt'):
outfile = "output.".join(os.path.basename(file_loc).split('.'))
with open(outfile, 'w') as w:
lines = open(file_loc, 'r').readlines() #everything is one giant list
w.write('\n'.join([line.split(':')[-1] for line in lines]))
ripple()
Try breaking this down, line by line, and changing things around. It's pretty condensed, but once you pick up comprehensions and using lists, it'll be more natural to read code this way.
You are trying to call .write() on a string object.
You either got your arguments mixed up (you'll need to call fileobject.write(yourdata), not yourdata.write(fileobject)) or you accidentally re-used the same variable for both your open destination file object and storing a string.

Categories

Resources