繁体   English   中英

使用现有 excel 文件的转换创建新的 excel 文件(使用 python)

[英]Creating new excel file with transformations from existing excel file (using python)

我有一个 xlsx 文件 df,其中包含大量数据。 我希望从特定单元格中提取数据并创建一个新的 xlsx 文件,其中包含此提取的数据以及日期。

这是文件,df:

    A   B   C                 #headers in excel
 
    1   2   3

期望输出:我希望从 C1(C 列,第 1 行)中提取数字 3,然后创建一个新文件 df2,如下所示 -

   Date        Value

   1/1/2020    3
            

这就是我正在做的:

import xlrd                                        #package for working with excel files
import xlwt                                        #allows you to create a new file
df = pd.read_excel(df.xlsx, sheetname="Sheet1")    #reading in my .xlsx file
worksheet = workbook.sheet_by_index(0)             #one sheet that I am iterating over
sheet.cell(0, 2).value                             #extracting the value in the first row, 2nd column
sheet.write(0, 2)                                  #Inserting data in 1st row and 2nd Column

但是,我坚持在新创建的文件中添加特定日期

任何建议表示赞赏

openpyxl是我最喜欢的 excel-python 进程库。 我已经将它用于我的公司项目,用于从 excel 导入和导出数据。

用于读取和写入 Excel(扩展名为 xlsx/xlsm/xltx/xltm)文件的 Python 库。

首先,要安装这个包,你需要终止这个命令:

sudo pip3 install openpyxl

让我们举一个例子来说明它是如何工作的。

输入 Excel 文件

Excel 演示数据

Python代码

打印第一列值

# importing openpyxl module 
import openpyxl 
  
# Give the location of the file 
path = "C:\\Users\\Admin\\Desktop\\demo.xlsx"
  
# workbook object is created 
wb_obj = openpyxl.load_workbook(path) 
  
sheet_obj = wb_obj.active 
m_row = sheet_obj.max_row 
  
# Loop will print all values 
# of first column  
for i in range(1, m_row + 1): 
    cell_obj = sheet_obj.cell(row = i, column = 1) 
    print(cell_obj.value)

输出

STUDENT 'S NAME
ANKIT RAI
RAHUL RAI
PRIYA RAI
AISHWARYA
HARSHITA JAISWAL

参考: 使用 Python openpyxl 模块读取 excel 文件

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM