我想用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/