I'm going to create a standalone python executable that rely on a python interpreter on windows (I don't want to create an exe file).
So, I'm used this helpful article:
https://n8henrie.com/2022/08/easily-create-almost-standalone-python-executables-with-the-builtin-zipapp-module/#google_vignette
My script python uses pywin32 for converting word to pdf,
After running this command:
> python -m pip install -t myapp pywin32
I tried to test working of the standalone script on myapp directory. so I ran:
> python .\myapp\main.py
And I get this error:
Traceback (most recent call last): File "D:\Parsa Projects\Python-PDF\myapp\main.py", line 3, in <module> import win32com.client File "D:\Parsa Projects\Python-PDF\myapp\win32com\__init__.py", line 8, in <module> import pythoncom File "D:\Parsa Projects\Python-PDF\myapp\pythoncom.py", line 2, in <module> import pywintypes ModuleNotFoundError: No module named 'pywintypes'
I searched about the problem and I found installing pypiwin32 solves the problem, so I Installed it on the myapp directory, but it not solved the problem.
Can anyone help me?
What I can do?
This is my python code:
import argparse
import pathlib
import win32com.client
import os
def convert_word_to_pdf(word_file_path, pdf_file_path):
if not os.path.exists(word_file_path):
raise FileNotFoundError(f"The file {word_file_path} does not exist.")
word = win32com.client.Dispatch('Word.Application')
word.Visible = False
try:
doc = word.Documents.Open(word_file_path)
doc.SaveAs(pdf_file_path, FileFormat=17)
doc.Close()
except Exception as e:
print(f"An error occurred: {e}")
finally:
word.Quit()
def main():
filepath = os.path.abspath('D:\\Parsa Projects\\Python-PDF\\file-sample_100kB.docx')
print(filepath)
# pdf_path = pathlib.Path('./file-sample_100kB.pdf').absolute()
pdf_path = os.path.abspath('D:\\Parsa Projects\\Python-PDF\\file-sample_100kB.pdf')
convert_word_to_pdf(filepath, pdf_path)
# parser = argparse.ArgumentParser("Arguments")
# if os.environ.get("MY_ENV_VAR") is not None:
# parser.add_argument("--my-env-var", required=True, action="store_true")
# if os.environ.get("MY_ENV_VAR2") is not None:
# parser.add_argument("--my-env-var2", required=True, action="store_true")
# parser.parse_args()
if __name__ == "__main__":
# main()
# exit()
# doc_path = pathlib.Path('./file-sample_100kB.docx').absolute()
filepath = os.path.abspath('D:\\Parsa Projects\\Python-PDF\\file-sample_100kB.docx')
print(filepath)
# pdf_path = pathlib.Path('./file-sample_100kB.pdf').absolute()
pdf_path = os.path.abspath('D:\\Parsa Projects\\Python-PDF\\file-sample_100kB.pdf')
convert_word_to_pdf(filepath, pdf_path)
For making the standalone python package, On this my directory D:\Parsa Projects\Python-PDF I opened cmd and ran:
> mkdir myapp
> xcopy /f /y main.py .\myapp\main.py
> python -m pip install -t myapp pywin32 pypiwin32
> python -m zipapp -p "C:\Users\Parsa\AppData\Local\Programs\Python\Python312\python.exe" -m main:main -c -o myapps myapp
cd myappthen callpython main.pydoes it work?ModuleNotFoundError: No module named 'pywintypes'pywin32package when it's installed in the local directory. (If you install a different package to/appdirand import it at the top of yourmain.pyand run it, it works as expected). Unfortunately I can't tell at a glance what the issue is--I know thatpywin32is indeed a strange package. I think it would take searching through the documentation of the python path based finder to understand why the package is not being found./myapp2) Executepip install -t myapp pywin323) Create/myapp/main.pywith a single lineimport pywin324) Runmain.pyand get the error