用python把数组的内容填到word的表格指定位置

时间: 2024-03-14 07:47:06 浏览: 20
可以使用Python中的`python-docx`库来实现将数组内容填充到Word表格中的指定位置。以下是一个示例代码: ```python import docx # 打开Word文档 doc = docx.Document('example.docx') # 获取第一个表格 table = doc.tables[0] # 要填充到表格中的数组 data = [['姓名', '性别', '年龄'], ['张三', '男', '20'], ['李四', '女', '22'], ['王五', '男', '25']] # 指定要填充数据的行列位置 row_idx = 1 # 第2行 col_idx = 0 # 第1列 # 遍历数组中的每一个元素,并填充到表格中指定的单元格中 for i in range(len(data)): for j in range(len(data[0])): cell = table.cell(row_idx+i, col_idx+j) cell.text = data[i][j] # 保存文档 doc.save('example.docx') ``` 在这个示例中,我们首先使用`docx.Document()`函数打开一个Word文档,然后使用`doc.tables[0]`获取文档中的第一个表格。接着,我们定义了一个二维数组`data`,表示要填充到表格中的数据。然后,我们指定要填充数据的行列位置,即第2行第1列的位置。接下来,我们使用两个嵌套的`for`循环遍历数组中的每一个元素,并将其填充到表格中指定的单元格中。最后,我们使用`doc.save()`函数保存文档。

相关推荐

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,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]))) # 输出每个主题下词语的概率分布至Excel表格 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 tf_feature_names = tf_vectorizer.get_feature_names() topic_word = print_topic_word_distribution(lda, tf_feature_names, n_top_words, n_topics) #print_topic_word_distribution(lda, tf_feature_names, n_top_words) output_topic_word_distribution_to_excel(lda, tf_feature_names, n_top_words, n_topics, "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, n_topics) File "D:\python\lda3\data_1.py", line 128, in print_topic_word_distribution dist = get_topic_word_distribution(lda, tf_feature_names,n_topics) TypeError: get_topic_word_distribution() takes 2 positional arguments but 3 were given

#模块导入 from selenium import webdriver from time import sleep from lxml import etree import xlwt import openpyxl #初始化 ##浏览器部分 bro = webdriver.Chrome() bro.get("https://china.nba.cn/players/stats/#!/stephen_curry") page_text = bro.page_source sleep(5) bro.quit() tree=etree.HTML(page_text) ##表格数据部分 file=xlwt.Workbook() sheet1 = file.add_sheet('sheet1',cell_overwrite_ok=True) line_per=[] #数据爬取函数准备 def score_record(x): tree_line=tree.xpath(x) line_0=[] for i in tree_line: tree_line_word=i.xpath('.//text()') for i in tree_line_word: if i.strip(): line_0.append(i.strip()) line_true=[line_0[0],line_0[18],line_0[19],line_0[20],line_0[21],line_0[22],line_0[23]] line_per.append(line_true) #爬取数据 a='/html/body/div[5]/div/div/div/div[2]/div[2]/section/div/div[2]/div[2]/div[1]/div[1]/div[3]/nba-stat-table/div/div[1]/table/thead/tr' score_record(a) for i in range(1,14): webside='/html/body/div[5]/div/div/div/div[2]/div[2]/section/div/div[2]/div[2]/div[1]/div[1]/div[3]/nba-stat-table/div/div[1]/table/tbody/tr[{}]'.format(i) score_record(webside) #保存数据 file = xlwt.Workbook() sheet1 = file.add_sheet('sheet1',cell_overwrite_ok=True) for j in range(0,14): for i in range(0,7): sheet1.write(j,i,line_per[j][i]) file.save('python结课程序.xls') #分析数据 sheet1.write(0,7,"两分出手") sheet1.write(0,8,"两分命中") wb = openpyxl.load_workbook('python结课程序.xls') sheet = wb['sheet1'] for i in range(2, 15): c_val = sheet.cell(row=i, column=3).value e_val = sheet.cell(row=i, column=5).value g_val = sheet.cell(row=i, column=7).value result = c_val - e_val - g_val sheet.cell(row=i, column=8).value = result for i in range(2, 15): b_val = sheet.cell(row=i, column=2).value d_val = sheet.cell(row=i, column=4).value f_val = sheet.cell(row=i, column=6).value result = b_val - d_val - f_val sheet.cell(row=i, column=9).value = result wb.save('python结课程序.xls')

最新推荐

recommend-type

使用python批量读取word文档并整理关键信息到excel表格的实例

今天小编就为大家分享一篇使用python批量读取word文档并整理关键信息到excel表格的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
recommend-type

python保存二维数组到txt文件中的方法

今天小编就为大家分享一篇python保存二维数组到txt文件中的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
recommend-type

Python 求数组局部最大值的实例

今天小编就为大家分享一篇Python 求数组局部最大值的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
recommend-type

python 实现在无序数组中找到中位数方法

1、求一个无序数组的中位数, (若数组是偶数,则中位数是指中间两个数字之和除以2,若数组是奇数,则中位数是指最中间位置。要求:不能使用排序,时间复杂度尽量低 2、例如: lists = [3, 2, 1, 4] , 中位数为 = ...
recommend-type

python 实现多维数组(array)排序

今天小编就为大家分享一篇python 实现多维数组(array)排序,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

list根据id查询pid 然后依次获取到所有的子节点数据

可以使用递归的方式来实现根据id查询pid并获取所有子节点数据。具体实现可以参考以下代码: ``` def get_children_nodes(nodes, parent_id): children = [] for node in nodes: if node['pid'] == parent_id: node['children'] = get_children_nodes(nodes, node['id']) children.append(node) return children # 测试数
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。