I'm trying to write a program that enables someone to edit html from python 'input()' questions. For example: change a paragraph from the command line in python. Is there some sort of library I can use to read html then edit and save it?
Since an HTML file is just a plain text file it can be opened by python without the need for any extra libraries and such. Just open the file, edit what you need and write it.
Check out the following link:
http://www.pythonforbeginners.com/files/reading-and-writing-files-in-python
Related
I am trying to create a xlsx from a template exported from Microsoft dynamics NAV, so I can upload my file to the system.
I am able to recreate and fill the template using the library xlsxwriter, but unfortunately I have figured out that the template file also have an attached XML source code file(visible in the developer tab in Excel).
I can easily modify the XML file to match what I want, but I can't seem to find a way to add the XML source code to the xlsx file.
I have searched for "python adding xlsx xml source" but it doesn't seem to give me anything I can use.
Any help would be greatly appreciated.
Best regards
Martin
Xlsx file is basically a zip archive. Open it as archive and you'll probably be able to find the XML file and modify it. –
Mak Sim
yesterday
I am trying to read .one file(OneNote files) and want to write its content into a text file, but didn't find a single way to do it using Python. Please help me with this.
Try to get the content of your notes calling:
./me/onenote/pages/1-1c13bcbae2fdd747a95b3e5386caddf1!1-xxxxxxxx-xxxx-xxxx-xxxxxxxxxxxx/content?includeIDs=true&includeInkML=true&preAuthenticated=true
It will give you text/html, which you can parse with https://pypi.org/project/lxml/
I didn't find a good way to decode .one file.
But I find another way to workaround it.
Install OneNote 2016 and sync the contents.
Install the Evernote legacy version
Import data from OneNote to Evernote. (There's a button on GUI).
Export notes to html from Evernote.
Then you can do whatever you want. Yeah!
This isn't Python, but for other internet voyagers trying to escape Microsoft's iron grip this PowerShell script is pure magic.
https://passbe.com/2019/bulk-export-onenote-2013-2016-pages-as-html/
I have the following code, which seems to serve a PDF without any content:
from pathlib import Path
pdf = Path("url/to/file.pdf")
print(f"Content-Type: application/pdf;\r\n")
print(pdf.read_bytes())
Any tips to correctly serve this PDF would be helpful!
Edit: for context, I am trying to serve PDF files and obscure original PDF file path on the server.
I'm no Python developer so I can't help you too specifically, but a couple things...
If your Phython script is outputting headers and content in the same response (such as via CGI), you need to have a blank line between the headers and content. Right now you have one \r\n. Add a second \r\n.
The other thing is that you should find a way to stream the output from that file rather than reading all its bytes and printing them.
Finally, I don't know if print() in Python is interpreting that as a string, but that can be problematic for binary data. This again is solved by piping a stream directly to the output.
Figured out the answer to my own question if anyone else needs to know:
pdf = Path("url/to/file.pdf")
print("Content-type: application/pdf\r\n\r\n")
stdout.flush()
stdout.buffer.write(pdf.read_bytes())
I realy dont know what your question is about. Its looks like code is unrelated with question. Its seems like its suppose to find pdf in local file system(build_in open?) and then print its content(?). Do you use some framework(flask/django)?
If by dynamic serving you have in mind dynamic creation of pdf based on some template:
pdf may be constructed from some markdown language like html, tex file(latex unfortunately is complicated system and dont fit to be depoloyed with web app)
markdown language file may be in turn rendered by template soft (jinja2, django build_in)
https://weasyprint.org/ is library that convert html + css to pdf
Ps. add more context
I have a script I have written in python which pulls data from a bunch of files on my computer which change daily. I want to insert the results into a latex template so that I can review the summary.
What is the best way to open a file and insert text into it at a specific point?
Preferably using a python, but I'm open to other tools if there is something better.
Thanks
Russ
You could also do it all from within python using the pylatex library.
https://github.com/JelteF/PyLaTeX
This way you only have to run the python file every day.
I figured out how to do it.
It seems to me the best way is to have python output low level latex code, then use latex's command \input to pull that code into a larger document.
https://en.wikibooks.org/wiki/LaTeX/Modular_Documents
I'm running into some strange problems with using BeautifulSoup for editing XML. After I read in the XML file and make all of my edits, I write it to a file using
output = open("output.xml", "r")
output.write(str(soup))
output.close
However, the program that needs to read in this XML file does not even recognize it exists (it's supposed to automatically load XML. For any reference this is ArcGis). I can open up the original XML and the Python-generated one and verify that they are completely identical--at least visually.
Another quirky behavior is that if I open the original XML file and make edits to it with a text editor and save, there is no error with the program and it loads correctly. If I make edits using Python but NOT with BeautifulSoup (i.e. string operations) it also works fine. it seems that writing the output string with BeautifulSoup causes the problem.
Is this some kind of encoding issue? What's the difference between a string obtained from the BeautifulSoup object and a normal string read straight from a file?