简体   繁体   English

是否可以使用Xlsxwriter从Python的Excel工作表中读取数据? 如果可以,怎么办?

[英]Is it possible to read data from an Excel sheet in Python using Xlsxwriter? If so how?

I'm doing the following calculation. 我正在做以下计算。

worksheet.write_formula('E5', '=({} - A2)'.format(number))

I want to print the value in E5 on the console. 我想在控制台上打印E5中的值。 Can you help me to do it? 你能帮我做吗? Is it possible to do it with Xlsxwriter or should I use a different library to the same? 是否可以使用Xlsxwriter做到这一点,还是应该使用其他库?

It is not possible to read data from an Excel file using XlsxWriter. 使用XlsxWriter无法从Excel文件读取数据。

There are some alternatives listed in the documentation . 文档中列出了一些替代方法

Not answering this specific question, just a suggestion - simply try pandas and read data from excel. 不回答这个特定问题, 只是一个建议 -只需尝试熊猫并从excel中读取数据即可。 Thereafter you can simply manipulate the data using pandas DataFrame built-in methods: 之后,您可以使用pandas DataFrame内置方法简单地操作数据:

df = pd.read_excel(file_,index_col=None, header=0)

df is the pandas.DataFrame, just go through DataFrame from this it's cookbook site . df是pandas.DataFrame,只需从此食谱网站浏览DataFrame。 If you are unaware about this package, you might get surprised by this awesome python module . 如果您不知道此软件包,则可能会对这个很棒的python模块感到惊讶。

If you want to use xlsxwriter for manipulating formats and formula that you can't do with pandas, you can at least import your excel file into an xlsxwriter object using pandas. 如果要使用xlsxwriter处理熊猫无法使用的格式和公式,则至少可以使用pandas将excel文件导入xlsxwriter对象。 Here's how. 这是如何做。

import pandas as pd
import xlsxwriter   

def xlsx_to_workbook(xlsx_in_file_url, xlsx_out_file_url, sheetname):
    """
    Read EXCEL file into xlsxwriter workbook worksheet
    """
    workbook = xlsxwriter.Workbook(xlsx_out_file_url)
    worksheet = workbook.add_worksheet(sheetname)
    #read my_excel into a pandas DataFrame
    df = pd.read_excel(xlsx_in_file_url)
    # A list of column headers
    list_of_columns = df.columns.values

    for col in range(len(list_of_columns)):
        #write column headers.
        #if you don't have column headers remove the folling line and use "row" rather than "row+1" in the if/else statments below
        worksheet.write(0, col, list_of_columns[col] )
        for row in range (len(df)):
            #Test for Nan, otherwise worksheet.write throws it.
            if df[list_of_columns[col]][row] != df[list_of_columns[col]][row]:
                worksheet.write(row+1, col, "")
            else:
                worksheet.write(row+1, col, df[list_of_columns[col]][row])
    return workbook, worksheet

# Create a workbook
#read you Excel file into a workbook/worksheet object to be manipulated with xlsxwriter
#this assumes that the EXCEL file has column headers
workbook, worksheet = xlsx_to_workbook("my_excel.xlsx", "my_future_excel.xlsx", "My Sheet Name")

###########################################################
#Do all your fancy formatting and formula manipulation here
###########################################################

#write/close the file my_new_excel.xlsx
workbook.close()

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

相关问题 如何在python中使用xlsxwriter从SQLITE导入数据到excel - how to import data from SQLITE to excel using xlsxwriter in python 使用xlsxwriter将数据从Dataframe写入Excel工作表时出错 - Error writing data from Dataframe to excel sheet using xlsxwriter 是否可以使用 Python 将 Excel 工作表中的数据推送到数据库中? - Is it possible to push data from Excel sheet using Python into a database? For循环使用python从硒中的Excel工作表中读取数据 - For loop to read data from excel sheet in selenium using python 如何在python中将相同的数据从Excel工作表读取到文本文件 - How to read the same data from excel sheet to the textfile in Python 如何使用 python 从单个 excel 表中读取两个表? - How to read two tables from single excel sheet using python? 如何使用 xlsxwriter 自定义 excel 表中按钮的操作 - How to customize the action of a button in excel sheet using xlsxwriter 如何使用 python 读取多个 excel 表 - How to read multiple excel sheet using python 如何使用XlsxWriter自动定义Excel工作表名称? - How to auto-define excel sheet name using XlsxWriter? 如何从多个 csv 文件中读取数据并写入 Python 中的单个 Excel 表的同一张表 - How to read data from multiple csv files and write into same sheet of single Excel Sheet in Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM