【问题标题】:Edit word (.docx) file using Python when it is opened in Word在 Word 中打开时使用 Python 编辑 word (.docx) 文件
【发布时间】:2020-11-28 16:38:19
【问题描述】:

我编写 python 脚本来编辑 MS Office Word 文档。在 Word 中打开 .docx 文件时编辑它对我来说非常重要。 Python 必须改变它。 我使用 python 库 docx 来做到这一点。代码工作正常,但在 MS Word 中打开时无法编辑 ms-word 文档。 我得到错误

PermissionError: [Errno 13] Permission denied: 'demo.docx'

为了解决这个问题,我尝试了这篇文章中推荐的所有方法:

PermissionError: [Errno 13] Permission denied

  1. 以管理员身份运行 cmd.exe
  2. 使用提升的权限创建快捷方式
  3. 更改 python 可执行文件的权限

没有任何帮助。我总是得到错误:

PermissionError: [Errno 13] Permission denied: 'demo.docx'

Python 代码无所谓,但我也在这里发布(代码有效,问题出在权限上):

from docx import Document
from docx.shared import Inches
import traceback 
try: 
    document = Document()
    document.add_heading('Document Title', 0)
    p = document.add_paragraph('A plain paragraph having some ')
    p.add_run('bold').bold = True
    p.add_run(' and some ')
    p.add_run('italic.').italic = True
    document.add_heading('Heading, level 1', level=1)
    document.add_paragraph('Intense quote', style='Intense Quote')
    document.add_paragraph(
        'first item in unordered list', style='List Bullet'
    )
    document.add_paragraph(
        'first item in ordered list', style='List Number'
    )
    #document.add_picture('monty-truth.png', width=Inches(1.25))
    records = (
        (3, '101', 'Spam'),
        (7, '422', 'Eggs'),
        (4, '631', 'Spam, spam, eggs, and spam')
    )
    table = document.add_table(rows=1, cols=3)
    hdr_cells = table.rows[0].cells
    hdr_cells[0].text = 'Qty'
    hdr_cells[1].text = 'Id'
    hdr_cells[2].text = 'Desc'
    for qty, id, desc in records:
        row_cells = table.add_row().cells
        row_cells[0].text = str(qty)
        row_cells[1].text = id
        row_cells[2].text = desc
    document.add_page_break()
    document.save('demo.docx')
except:
    traceback.print_exc() 

【问题讨论】:

  • 您问的是如何打破操作系统与 MS Word 应用程序签订的合同,当它授予它对文件的独占写入访问权限时,以便您的程序可以进行并发更改?
  • @kjhughes 我在这个领域没有太多的知识。你的回答意味着这是不可能的? VBA-Python 绑定可能有帮助吗?
  • 我将把“不可能”的声明留给那些具有详细的 Windows 系统编程专业知识的人,但要意识到您正在尝试做的事情与 MS Word 应用程序已打开具有独占写入权限的文档。您收到的“权限被拒绝”错误是操作系统说它已经放弃了对另一个程序的写访问权限 - 您也不能拥有它。
  • 你考虑过写add-in吗?
  • @kjhughes 现在我尝试用 xlwings 来做。但我不知道它会不会有帮助。docs.xlwings.org/en/0.21.4/index.html

标签: python ms-word docx python-docx


【解决方案1】:

如 cmets 中所述,您不能修改已使用其他应用程序(即:python-docx 模块)打开的 Word 文档。

但是,您可以使用 win32api / pywin32 直接访问 Word(与 Excel 相同)。

您可以使用类似的语法(只是翻译成 Python)来做所有可以用 VBA 做的事情。

它又重又丑,但至少它有效。

这是来自此博客的代码示例:https://www.blog.pythonlibrary.org/2010/07/16/python-and-microsoft-office-using-pywin32/

import win32com.client as win32

RANGE = range(3, 8)

def edit_word():
    word = win32.gencache.EnsureDispatch('Word.Application')
    doc = word.Documents.Add()
    word.Visible = True
    rng = doc.Range(0,0)
    rng.InsertAfter('Hacking Word with Python\r\n\r\n')
    sleep(1)
    for i in RANGE:
        rng.InsertAfter('Line %d\r\n' % i)
        sleep(1)
    rng.InsertAfter("\r\nPython rules!\r\n")
    doc.Close(False)
    word.Application.Quit()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多