Convert Word file to pdf in python [duplicate] - python

This question already has answers here:
Convert Microsoft Word document to PDF using Python
(3 answers)
Closed 28 days ago.
How do I go about converting a word file to a pdf in python? I use replit, so I realise that rules out all the options that require linux to install Office
Many thanks,
ideally, this is what i expect
input(DOCUMENT.docx)
document.docx.convert(pdf)
output(DOCUMENT.pdf)

Just confirmed that the following works in Linux:
import subprocess
import os
wordfile = "yourword.docx"
command = ["lowriter", "--headless", "--convert-to", "pdf", wordfile]
subprocess.call(command)
if os.path.exists("yourword.pdf"):
print("Done")

Related

How to handle windows path in Python? [duplicate]

This question already has answers here:
How should I write a Windows path in a Python string literal?
(5 answers)
Closed 15 days ago.
The requirement of code is I have to take the file path from the user in the console and perform some action on that file. Users can give paths in windows style or mac style. for the mac or Linux, the path code is working fine but for the windows path its give an error (because of ) how to handle this error as I can't use the 'r' string in that as it's coming from the user.
user_path = input('give text file path: ')
file = open(user_path, 'r')
words = file.read().split()
print('total number of words: ', len(words))
And if I provide path: C:\desktop\file.txt
its give error
Use C:\\desktop\\file.txt instead of C:\desktop\file.txt.
The error is due to the fact that python is recognizing '\f' in 'C:\desktop\file.txt' as the form feed escape sequence.
It can simply be resolved by using forward slash '/' instead of backslash while providing input.

How to see Windows Version with Python? [duplicate]

This question already has answers here:
How to identify which OS Python is running on?
(27 answers)
Closed 3 years ago.
I am making a python program which would act as windows cmd, but I need a code which will display Windows's version. [Highlighted one]
How to make it?
import sys
ver = sys.getwindowsversion()
print("{}.{}.{}".format(ver.major, ver.minor, ver.build))
Output :
10.0.18362
import platform
platform.version()
My output was '10.0.17134'
You can try this:
import os
os.system('ver')

Convert oga (Telegram audio file fpormat) to ogg/wav file format using Python [duplicate]

This question already has answers here:
How to convert Telegram voice in a wave file in python
(2 answers)
Closed 3 years ago.
Not able to proceed ahead.
We get .oga file from record in Telegram app.
Which needs to be converted to wav/ogg file format for further processing.
Taking in consideration, that this answer is purely academic, I had not try it. You can use ffmpeg, and run it through python with subprocess.
import subprocess
src_filename = 'source.ext1'
dest_filename = 'output.ext2'
process = subprocess.run(['ffmpeg', '-i', src_filename, dest_filename])
NOTE: Looking around, I found this that may be the same that you asked.

Get system file type names python [duplicate]

This question already has answers here:
How can I check the extension of a file?
(14 answers)
How to check type of files without extensions? [duplicate]
(10 answers)
Closed 4 years ago.
Is there any way to retrieve a file type name in Python using the OS module?
An example made up command:
>>> os.file_type('txt')
Would return:
'Text Document'
Any help would be appreciated :)
Oscar.
For getting file type you need to check the extension of the file
I think this can help.
import os
if os.path.splitext(file)[1] == ".txt":
pritn 'Text Document'
For os related task you can look over this doc.
https://github.com/Projesh07/Python-basic/blob/master/python_os_module/python_os_module.py

SyntaxError: Invalid Syntax in python when trying to use tarfile.open() on a .tgz file [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Syntax error on print with Python 3
I want to view the contents of a .tgz file and I found python's tarfile module. I found the following tutorial which looked promising. http://www.doughellmann.com/PyMOTW/tarfile/
Here is my python file below:
import tarfile
tar = tarfile.open("exampleTar.tgz","r")
print tar.getnames()
When I actually execute my python file, I get a carrot sign pointing at the 'r' in the last line and the error message: SyntaxError: invalid syntax.
Print is function in python 3.x.
import tarfile
tar = tarfile.open("exampleTar.tgz","r")
print(tar.getnames())

Categories