xlsxwriter

Writing heirarchical JSON data to Excel xls from Python?

一个人想着一个人 提交于 2019-12-06 14:50:20
问题 I want to write some data from python to xlsx. I currently have it stored as JSON, but it doesn't matter what it is going out of Python. Here's what the JSON for a single article would look like: { 'Word Count': 50 'Key Words': { ['Blah blah blah', 'Foo', ... ] } 'Frequency': { [9, 12, ... ] } 'Proper Nouns': { ['UN', 'USA', ... ] } 'Location': 'Mordor' } I checked out the XlsxWriter module but can't figure out how to translate hierarchical data that is not necessarily the same size (note the

Pandas to Excel conditional formatting whole column

早过忘川 提交于 2019-12-06 13:55:25
I want to write a Pandas dataframe into Excel with formatting. For this I'm using xlsxwriter . My question is twofold: First, how can I apply conditional formatting to a whole column? In the examples they use specific cell-range formatting (eg. worksheet1.conditional_format('B3:K12', ...) ), is there a way I can reference a whole column? Second, I want to mark the column B with red if it differs from column C by more than 15%. What would be the correct formula for this? I'm currently trying the below code, which doesn't work (I don't know how to reference columns). writer = pd.ExcelWriter(

自动化办公:python操作Excel

邮差的信 提交于 2019-12-06 12:14:44
转载原链接: https://www.jianshu.com/p/d685cfaaeef7 1.安装 pip install xlsxwriter 2.操作一个简单的Excel文档 # 引入依赖模块 import xlsxwriter # 数据准备 datas = ( ['Rent', 1000], ['Gas', 100 ], ['Food', 300 ], ['Gym', 50 ] ) # 创建一个Excel文档 workbook = xlsxwriter.Workbook('ex01.xlsx') # 添加一个工作表 worksheet = workbook.add_worksheet() # 设置行和列的偏移 row, col = 0, 0 # 开始添加数据 for item, cost in datas: # 指定行、列的单元格,添加数据 worksheet.write(row, col, item) worksheet.write(row, col+1, cost) # 行增加 row += 1 # 添加一个计算总数的函数 worksheet.write(row, 0, 'Total') worksheet.write(row, 1, '=SUM(B1:B4)') # 关闭文档 workbook.close() 操作完成后,数据存储结果如下:

Pandas: Iterate through a list of DataFrames and export each to excel sheets

两盒软妹~` 提交于 2019-12-06 07:42:20
Trying to teach myself coding to automate some tedious tasks at work. I apologize for any unintentional ignorance. I have created Data Frames in pandas (python 3.x). I want to print each data frame to a different excel sheet. Here is what I have for 2 Data Frames, it works perfect but I want to scale it to loop through a list of data frames so that I can make it a bit more dynamic. writer = pandas.ExcelWriter("MyData.xlsx", engine='xlsxwriter') Data.to_excel(writer, sheet_name="Data") ByBrand.to_excel(writer, sheet_name="ByBrand") writer.save() Easy enough, but when there are 50+ sheets that

How can I remove the green arrow from a cell on excel file using python-xlsxwriter?

a 夏天 提交于 2019-12-05 17:22:42
When i wrote a value that does have a percentage sign, excel shows me a green arrow on the top of that cell. This is what I use to write a value in a specific cell. worksheet.write(1, 46, '12%') I tried this : worksheet.write_string(1, 46, '12%') and this worksheet.write_number(1, 46, '12%') but I get the same results. How can i get rid of the green arrow? Thanks! The green arrow/triangle is an Excel warning. It is probably the warning about numbers stored as text. The way to avoid this is to write the number without the percentage and then format it with a number format so that the percentage

xlsxwriter: add formula with other sheet in it

十年热恋 提交于 2019-12-05 16:44:57
I try to create a XLSX file with xlsxwriter python plugin. In this XLSX, I have 2 sheets: Analyse : Contain a table with informations Stat : Contain some informations and 2 formulas This 2 formulas are: =NBVAL(Analyse!C:C)-1 =NB.SI(Analyse!D:D;"To change") My problem is when I open the generated file, I have a error. And the formulas don't work. If I edit the formula and just press Enter, it work. My code: shInfo = self.__workbook.add_worksheet("Stat") shInfo.activate() information = self.__workbook.add_format({'bg_color': '#BFBFBF', 'font_name': 'Courier New'}) shInfo.write('G3','=NBVAL

Dynamically produced XLSXWriter charts in python - not referencing

别来无恙 提交于 2019-12-05 14:08:39
I'm using the below class I've written to try and dynamically create a single Excel file with several worksheets where there is a printed dataframe and a column chart in each worksheet. Interacton with the code (seen below) should function where you initiate a workbook: test = Workbook('Test Workbook') And then, you can add as many charts as you want: test.add_chart(df, 'Df Title', 1) test.add_chart(df2, 'Df2 Title', 1) And then you produce the workbook: test.produce() Input dataframes have headers. First column is text categories, subsequent columns (of varying number) are data in the form of

Alternating row color using xlsxwriter in Python 3

时间秒杀一切 提交于 2019-12-05 11:31:59
Has anybody implemented alternating row color while generating excel using xlsxwriter in Python3? data_format = workbook.add_format( { 'bg_color': '#FFC7CE' }) worksheet.write(data_row, data_col + 1, row[1], data_format) This sets the color for each column. There is nothing stopping you from setting the formats manually as follows. A context manager is used to automatically close the workbook afterwards. import xlsxwriter with xlsxwriter.Workbook('hello.xlsx') as workbook: worksheet = workbook.add_worksheet() data_format1 = workbook.add_format({'bg_color': '#FFC7CE'}) data_format2 = workbook

Setting default number format when writing to Excel from Pandas

拜拜、爱过 提交于 2019-12-05 05:37:13
I'm looking to set the default number format when writing to Excel from a Pandas dataframe. Is this possible? I can set the default date/datetime_format with the following, but couldn't find a way to set the default number format. writer = pd.ExcelWriter(f'{file_variable}.xlsx', engine='xlsxwriter',datetime_format='MM/DD/YYYY') Otherwise, I assume I'm going to have to assign worksheets to variables and loop through the rows for the specified columns to set the number format. Dickster I got this format the floats to 1 decimal place. data = {'A Prime': {0: 3.26, 1: 3.24, 2: 3.22, 3: 3.2, 4: 3.18

Possible to alter worksheet order in xlsxwriter?

别等时光非礼了梦想. 提交于 2019-12-05 01:29:17
I have a script which creates a number of the following pairs of worksheets in order: WorkSheet (holds data) -> ChartSheet using WorkSheet After the script is finished, I am left with worksheets ordered as such: Data1, Chart1, Data2, Chart2, Data3, Chart3, ... Is it possible to re-order the worksheets at the end of the script (i.e. before workbook.close() ) to obtain the following worksheet order in the final .xlsx file? Chart1, Chart2, Chart3,...,ChartN, Data1, Data2, Data3,... Just sort workbook.worksheets_objs list: import xlsxwriter workbook = xlsxwriter.Workbook('test.xlsx') sheet_names =