Cell In[252], line 280 277 doc.save(r'D:\好记薪\工商资料生成\模板\转让前股东决定定稿.docx') 279 if sheet2['F3'].value: --> 280 doc = Document(r'D:\好记薪\工商资料生成\4.转让前股东会决议模板.docx') 281 paragraph = doc.paragraphs[2] 282 paragraph.add_run('\t二、股权转让后,公司股东持股情况如下:') File D:\Program Files\anaconda\lib\site-packages\docx\api.py:25, in Document(docx) 18 """ 19 Return a |Document| object loaded from *docx*, where *docx* can be 20 either a path to a ``.docx`` file (a string) or a file-like object. If 21 *docx* is missing or ``None``, the built-in default document "template" 22 is loaded. 23 """ 24 docx = _default_docx_path() if docx is None else docx ---> 25 document_part = Package.open(docx).main_document_part 26 if document_part.content_type != CT.WML_DOCUMENT_MAIN: 27 tmpl = "file '%s' is not a Word file, content type is '%s'"
时间: 2024-03-29 17:34:20 浏览: 179
这段代码出现错误了,报错信息显示是在第280行,即在读取文件时出现了问题。可能原因是文件路径不正确,或者文件不存在,或者文件格式不是docx格式。需要检查文件路径是否正确、文件是否存在以及文件格式是否正确。如果文件路径不正确,可以修改为正确的路径,如果文件不存在或者文件格式不正确,需要重新获取正确的文件。
相关问题
n_topics = 10 lda = LatentDirichletAllocation(n_components=n_topics, max_iter=50, learning_method='batch', learning_offset=50, #doc_topic_prior=0.1, #topic_word_prior=0.01, random_state=0) lda.fit(tf) ###########每个主题对应词语 import pandas as pd from openpyxl import Workbook # 获取主题下词语的概率分布 def get_topic_word_distribution(lda, tf_feature_names): arr = lda.transform(tf_vectorizer.transform([' '.join(tf_feature_names)])) return arr[0] # 打印主题下词语的概率分布 def print_topic_word_distribution(lda, tf_feature_names, n_top_words): dist = get_topic_word_distribution(lda, tf_feature_names) for i in range(lda.n_topics): print("Topic {}: {}".format(i, ', '.join("{:.4f}".format(x) for x in dist[i]))) # 输出每个主题下词语的概率分布至Excel表格 def output_topic_word_distribution_to_excel(lda, tf_feature_names, n_top_words, filename): # 创建Excel工作簿和工作表 wb = Workbook() ws = wb.active ws.title = "Topic Word Distribution" # 添加表头 ws.cell(row=1, column=1).value = "Topic" for j in range(n_top_words): ws.cell(row=1, column=j+2).value = tf_feature_names[j] # 添加每个主题下词语的概率分布 dist = get_topic_word_distribution(lda, tf_feature_names) for i in range(lda.n_topics): ws.cell(row=i+2, column=1).value = i for j in range(n_top_words): ws.cell(row=i+2, column=j+2).value = dist[i][j] # 保存Excel文件 wb.save(filename) n_top_words = 30 tf_feature_names = tf_vectorizer.get_feature_names() topic_word = print_topic_word_distribution(lda, tf_feature_names, n_top_words) #print_topic_word_distribution(lda, tf_feature_names, n_top_words) output_topic_word_distribution_to_excel(lda, tf_feature_names, n_top_words, "topic_word_distribution.xlsx")报错Traceback (most recent call last): File "D:\python\lda3\data_1.py", line 157, in <module> topic_word = print_topic_word_distribution(lda, tf_feature_names, n_top_words) File "D:\python\lda3\data_1.py", line 129, in print_topic_word_distribution for i in range(lda.n_topics): AttributeError: 'LatentDirichletAllocation' object has no attribute 'n_topics'
这个错误提示的意思是说 `LatentDirichletAllocation` 对象没有 `n_topics` 这个属性。你可以尝试将 `n_topics` 直接传递给函数,而不是从 `lda` 对象中获取。修改代码如下:
```
def print_topic_word_distribution(lda, tf_feature_names, n_top_words, n_topics):
dist = get_topic_word_distribution(lda, tf_feature_names, n_topics)
for i in range(n_topics):
print("Topic {}: {}".format(i, ', '.join("{:.4f}".format(x) for x in dist[i])))
def output_topic_word_distribution_to_excel(lda, tf_feature_names, n_top_words, n_topics, filename):
# 创建Excel工作簿和工作表
wb = Workbook()
ws = wb.active
ws.title = "Topic Word Distribution"
# 添加表头
ws.cell(row=1, column=1).value = "Topic"
for j in range(n_top_words):
ws.cell(row=1, column=j+2).value = tf_feature_names[j]
# 添加每个主题下词语的概率分布
dist = get_topic_word_distribution(lda, tf_feature_names, n_topics)
for i in range(n_topics):
ws.cell(row=i+2, column=1).value = i
for j in range(n_top_words):
ws.cell(row=i+2, column=j+2).value = dist[i][j]
# 保存Excel文件
wb.save(filename)
n_top_words = 30
n_topics = 10
tf_feature_names = tf_vectorizer.get_feature_names()
topic_word = print_topic_word_distribution(lda, tf_feature_names, n_top_words, n_topics)
output_topic_word_distribution_to_excel(lda, tf_feature_names, n_top_words, n_topics, "topic_word_distribution.xlsx")
```
这样应该就能够正常执行了。
写一个python代码,实现需求: 1、打开output.txt文件; 2、搜索output.tx文档的内容,找到"修改后的内容:",将"修改后的内容:"往后的每一行存储到列表result[]中; 3、继续搜索,找到"影响范围:",结束存储; 4、打开模板.docx文档,找到里面的第二个表格doc.tables[1]; 5、将result[]中的内容依次填入doc.tables[1]第13行开始的第3列中; 6、如果doc.tables[1]中12至80行
都已经有内容,就将result[]中的内容插入到doc.tables[1]的最后一行,然后保存模板.docx文件。以下是代码实现:
```python
import docx
# 打开output.txt文件,搜索"修改后的内容:"和"影响范围:"
with open('output.txt', 'r') as f:
result = []
flag = False
for line in f.readlines():
if flag:
if line.startswith('影响范围:'):
break
else:
result.append(line.strip())
elif line.startswith('修改后的内容:'):
flag = True
# 打开模板.docx文件,找到第二个表格doc.tables[1]
doc = docx.Document('模板.docx')
table = doc.tables[1]
# 如果doc.tables[1]中12至80行都已经有内容,将result[]中的内容插入到最后一行
if all(table.cell(i, 2).text != '' for i in range(12, 81)):
for item in result:
row_cells = table.add_row().cells
row_cells[2].text = item
# 否则,将result[]中的内容依次填入doc.tables[1]第13行开始的第3列中
else:
for i, item in enumerate(result):
table.cell(i+12, 2).text = item
# 保存模板.docx文件
doc.save('模板.docx')
```
阅读全文