在 Word 中打开时使用 Python 编辑 word (.docx) 文件

伊戈尔K。

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

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

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

PermissionError: [Errno 13] 权限被拒绝

  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() 
让-弗朗索瓦·T。

如评论中所述,您无法修改已使用其他应用程序(即: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()

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Word无法使用表打开DOCX文件

使用python从MS word docx文件中逐页提取文本

Python使用docx库在Word文件中编写

如何使用 Microsoft.Office.Interop.Word 在 C# 中编辑 docx 文件

使用python-docx识别Word文档.docx文件中的封面

是否可以使用 Python 编辑 MS word doc 文件?

重新压缩的docx文件无法在Word中打开

SharePoint CSOM:在Word Online(Office 365)中打开.docx文件

使用python docx合并word文档

使用Java,如何使Word打开和编辑文件?

在Windows Form C#上打开Word(.docx)文件

python -docx从word docx提取表

使用Python处理具有链接并跟踪更改的Microsoft Word DOCX文件

如何使用python编辑导入的Word文档

如何在Word 2007 .docx文件中搜索单词?

在Midnight Commander中查看MS Word .docx文件

使用 Apache POI Word JAVA 编写 docx 文件

使用Python docx在Word中创建语音提示/“ Ruby文本”?

使用Python-docx无法在Word的ContentControl中获取文本

如何使用Python Docx更新MS Word中的字段

使用python docx遍历word文档中的表

如何生成,导出到Word docx文件?

在Word表中搜索某些文本Python docx

如何用python中的Word docx替换Latex Bold命令?

使用Python更新MS Word .docx文档的目录(目录)

如何使用python将word docx转换为图像

Python无法编辑Word文档

使用Python-docx编写word文档时如何更改段落中特定文本的字体?

您用什么来编辑Microsoft Word文档(docx)?