python-3.x - 使用 win32com 库会带来隐私问题吗?

标签 python-3.x

我想用python做一些简历信息的提取。一些简历以 .doc 扩展名出现,我有一个来自另一个答案的代码,用于将这些 .doc 文件转换为 .docx 文件。代码如下:

import win32com.client as win32
from win32com.client import constants

def save_as_docx(path):

    word = win32.gencache.EnsureDispatch('Word.Application')
    doc = word.Documents.Open(path)
    doc.Activate ()

    new_file_abs = os.path.abspath(path)
    new_file_abs = re.sub(r'\.\w+$', '.docx', new_file_abs)

    word.ActiveDocument.SaveAs(
        new_file_abs, FileFormat=constants.wdFormatXMLDocument
    )
    doc.Close(False)

save_as_docx(some_path)

我真的不明白 win32com.client 是如何工作的,所以在实际情况下使用这段代码之前,我的问题是:

此代码是否将简历上传到某处以便将其转换为 .docx?使用这个会不会有什么隐私或者信息泄露问题需要注意?

提前致谢。

最佳答案

此代码不会将简历上传到任何地方。我没有看到你应该对此有隐私问题。您可以在 https://en.wikipedia.org/wiki/Component_Object_Model 阅读有关 COM 的更多信息但简而言之,COM 是 Windows 上的一个系统,旨在帮助不同的应用程序进行通信。几乎 Microsoft 应用程序所做的任何事情也可以使用 COM 来完成,但通常很难弄清楚到底是怎么做的。在 Windows 上的 Python 中,win32com是一种访问 native COM 接口(interface)的方法,client对于一个进程是本地的,并与本地计算机上的另一个进程通信。

注释您的代码:

import win32com.client as win32
from win32com.client import constants

def save_as_docx(path):
    # Access the Word application
    word = win32.gencache.EnsureDispatch('Word.Application')
    # Cause word to open a document
    doc = word.Documents.Open(path)

    # Make the document the active document, so the rest of the commands apply to it
    # https://learn.microsoft.com/en-us/office/vba/api/word.document.activate
    doc.Activate ()

    # make the path absolute, replace the `doc` with `docx`
    new_file_abs = os.path.abspath(path)
    new_file_abs = re.sub(r'\.\w+$', '.docx', new_file_abs)

    # Save the document in the docx format at a new location
    word.ActiveDocument.SaveAs(
        new_file_abs, FileFormat=constants.wdFormatXMLDocument
    )

    # Close the document
    doc.Close(False)

save_as_docx(some_path)

你可以阅读它并看到 COM 正在检测 Word 应用程序,它允许你自动化一些你可能认为只能通过 GUI 完成的事情。但请放心,这一切都在您的机器上本地完成,除非出于某种原因 Word 应用程序正在以某种方式为您上传文档。

关于python-3.x - 使用 win32com 库会带来隐私问题吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57531798/

相关文章:

python-3.x - 如何将命令行参数从pytest传递给代码

python - 有什么方法可以使这个 if 语句更短更清晰吗?

python-3.x - 参数 "--python-modules-installer-option"在 pythonshell Glue Jobs 中不起作用

python-3.x - 推荐的方式来分发我的 python 包而不上传到 PyPi

python - 实现 jsonpickle 自定义处理程序时如何处理单个值?

python - 如何使用 Panda 将 JSON 字符串中的所有元素输出到表中

python - 尽管遵循安装说明,Beaker 仍无法找到 Python 和 Julia 安装

python - 在python中将32位二进制转换为十进制

Python - matplotlib - subplot() 和 subplots() 之间的区别

python Pandas : How do I create a column given a condition based on another column?