马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 YouCam 于 2023-6-23 15:08 编辑
以下是源代码:import openpyxl
import docx
the_doc = docx.Document(r"C:\Users\Qingxi\Desktop\sentence.docx")
the_sheet = openpyxl.load_workbook('C:/Users/Qingxi/Desktop/word-list.xlsx').active
the_list = []
for i in range(1, int(the_sheet.max_row) + 1):
smalllist = []
smalllist.append(the_sheet.cell(row = i, column = 1).value)
smalllist.append(the_sheet.cell(row = i, column = 2).value)
the_list.append(smalllist)
words = []
count = 1
for i in the_list:
if i[1] != None and i[0] in i[1]:
sentence = i[1].replace(i[0],'____')
words.append(i[0])
if words.count > 3:
words.remove()
the_doc.add_paragraph("Can you finish this sentence? # DAY " + str(count))
the_doc.add_paragraph(sentence)
the_doc.add_paragraph(' '.join(words))
the_doc.add_paragraph()
count += 1
the_doc.save()
以下是报错提示:
PS F:\Code\Python> & C:/Users/Qingxi/AppData/Local/Programs/Python/Python311/python.exe f:/Code/Python/Docx.py
Traceback (most recent call last):
File "f:\Code\Python\Docx.py", line 4, in <module>
the_doc = docx.Document(r"C:\Users\Qingxi\Desktop\sentence.docx")
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Qingxi\AppData\Local\Programs\Python\Python311\Lib\site-packages\docx\api.py", line 25, in Document
document_part = Package.open(docx).main_document_part
^^^^^^^^^^^^^^^^^^
File "C:\Users\Qingxi\AppData\Local\Programs\Python\Python311\Lib\site-packages\docx\opc\package.py", line 128, in open
pkg_reader = PackageReader.from_file(pkg_file)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Qingxi\AppData\Local\Programs\Python\Python311\Lib\site-packages\docx\opc\pkgreader.py", line 32, in from_file
phys_reader = PhysPkgReader(pkg_file)
^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Qingxi\AppData\Local\Programs\Python\Python311\Lib\site-packages\docx\opc\phys_pkg.py", line 30, in __new__
raise PackageNotFoundError(
docx.opc.exceptions.PackageNotFoundError: Package not found at 'C:\Users\Qingxi\Desktop\sentence.docx'
PS F:\Code\Python>
经过众多网友的热心帮助,我在顶楼说明一下情况:
1. 我能确保路径是正确的,在桌面文件夹下确实有一个名为sentence.docx的文件
2. 文件扩展名是一致的,和代码中的 sentence.docx一致
3. 在填写路径时我加了 r 在路径前面
4. 当前电脑是Windows 11 系统,使用的编辑器是VS code,并授予了Trust this folder的权限
5. 此docx文档是使用Microsoft 365 创建的,并非offcie,Microsoft 365没有损坏,可以打开其他docx文件
6. 此文档从未设置过密码保护
7. 我的python 版本是 3.11.3,docx 扩展库版本 0.2.4,python_docx版本 0.8.11, openpyxl 版本 3.1.2
8. 此sentence.docx无程序占用
|