简体   繁体   English

使用 xlsxwriter 将不同工作表中的数据用于表格

[英]Use data from a different sheet for table using xlsxwriter

I am using xlsxwriter to generate a file with quite a few formulas.我正在使用 xlsxwriter 生成一个包含很多公式的文件。 From there, I want to create a table on another sheet.从那里,我想在另一张纸上创建一个表格。 Everything is pretty straightforward until I want to use data from a different sheet for the table.在我想为表格使用不同工作表中的数据之前,一切都非常简单。

The documentation only shows examples of already having the data you need, and then passing that to the.add_table as the 'data' parameter.该文档仅显示已经拥有所需数据的示例,然后将其作为“数据”参数传递给.add_table。

What I am trying to do is this: (Which is structured how the rest of xlsxwriter's formulas are.)我想要做的是:(它的结构是 xlsxwriter 公式的 rest 的结构。)

df = pd.DataFrame(stuff)
writer = pd.ExcelWriter('File.xlsx', engine = 'xlsxwriter')
df.to_excel(writer, sheet_name='Sheet1')
workbook = writer.book
worksheet1 = writer.sheets['Sheet1']
worksheet2 = workbook.add_worksheet('Summary Page')
data = f"'Sheet1'!$A$1:$D${len(df)}"
worksheet2.add_table(f'A1:D{len(df)}', {'data':data})
workbook.close()

This approach adds the new sheet, and creates a table the correct size.这种方法会添加新工作表,并创建正确大小的表格。 But then fills in the "data" with 'data' as a string down the first column with one character in each cell.但随后将“数据”与“数据”作为字符串填充到第一列中,每个单元格中都有一个字符。

Is there a way to create a table referencing data from another sheet using xlsxwriter?有没有办法使用 xlsxwriter 创建一个引用来自另一个工作表的数据的表?

ExcelWriter is (obviously) for writing Excelfiles. ExcelWriter (显然)用于编写 Excel 文件。 If you want to read data from Excel after writing and saving it (did I get you right?!) use ExcelFile.parse or read_excel to convert data to dataframe and write it again to Excel by ExcelWriter .如果您想在写入并保存后从 Excel 读取数据(我没听错吗?!)使用ExcelFile.parseread_excel将数据转换为 dataframe 并通过ExcelWriter再次写入 ZC1D81AF5835844B4EZD9D93F96。 Unfortunately xlsxwriter does not support appending, so you have to load and write all sheets again.不幸的是 xlsxwriter 不支持追加,因此您必须再次加载和写入所有工作表。 Or just use the default openpyxl as engine.或者只是使用默认的openpyxl作为引擎。 Could be omitted (as said: default) but to point out it is given in minimal working example:可以省略(如前所述:默认),但要指出它是在最小的工作示例中给出的:

import pandas as pd

df = pd.DataFrame({'Data': [10, 20, 30, 20, 15, 30, 45]})
writer = pd.ExcelWriter('test.xlsx', engine='openpyxl')
df.to_excel(writer, sheet_name='Sheet1')
writer.save()

data = pd.read_excel('test.xlsx', usecols='A:B', sheet_name='Sheet1', index_col=0)
writer = pd.ExcelWriter('test.xlsx', engine='openpyxl', mode='a')
# shape our data here
data.to_excel(writer, sheet_name='Sheet2')
writer.save()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用xlsxwriter将数据从Dataframe写入Excel工作表时出错 - Error writing data from Dataframe to excel sheet using xlsxwriter 是否可以使用Xlsxwriter从Python的Excel工作表中读取数据? 如果可以,怎么办? - Is it possible to read data from an Excel sheet in Python using Xlsxwriter? If so how? 如何使用xlsxwriter将数据写入不同的工作表? - How to write data to different sheets using xlsxwriter? Xlsxwriter 和 Sparklines 以及来自另一张表的数据,渲染问题 - Xlsxwriter & Sparklines With Data From Another Sheet, Render Issue Xlsxwriter - 将表中的变量用于 excel - Xlsxwriter - use variable in table to excel 使用 xlsxwriter 在现有 Excel 工作表中插入图像 - Insert image in existing excel sheet by using xlsxwriter 有没有办法在 XlsxWriter 中创建图表而不将数据写入工作表中的单元格? IE。 来自 python 脚本中的数组? - Is there a way to create a chart in XlsxWriter without writing the data to cells in a sheet? IE. From an array in the python script? 使用来自 xlsxwriter 的 Workbook 对象时,Workbook 对象没有属性“add_sheet” - Workbook object has no attribute 'add_sheet' when using Workbook object from xlsxwriter 使用Python从xml文件中提取数据并写入xlsxwriter - Extracting data from xml file using Python and writing to xlsxwriter 如何在python中使用xlsxwriter从SQLITE导入数据到excel - how to import data from SQLITE to excel using xlsxwriter in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM