[英]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.