openpyxl

openpyxl(一)

与世无争的帅哥 提交于 2019-12-02 13:01:46
路由表一般是用Excel格式来编写的。因此需要用到openpyxl模块。 每个Excel文档打开之后可以抽象为一个workbook(工作簿),每个工作簿下面又有很多的sheet(工作表),sheet里面又有很多的Cell(表格),cell又可以组成行和列。 首先看怎么打开一个Excel文档: from openpyxl import load_workbook wb = load_workbook(filename = 'empty_book.xlsx') sheet=wb.get_sheet_by_name('RoutingTable') for row in sheet.rows: for cell in row: print(cell.value) 然后遍历一下所有cell的内容。 来源: https://www.cnblogs.com/ichbinhere/p/11746140.html

How to separate extracted column from excel into different lists or groups in Python

和自甴很熟 提交于 2019-12-02 11:30:20
I am reading a column from an excel file using openpyxl. I have written code to get the column of data I need into excel but the data is separated by empty cells. I want to group these data wherever the cell value is not None into 19 sets of countries so that I can use it later to calculate the mean and standard deviation for the 19 countries. I don't want to hard code it using list slices. Instead I want to save these integers to a list or list of lists using a loop but im not sure how to because this is my first project with Python. Here's my code: #Read PCT rankings project ratified results

Graphs lost while overwriting to existing excel file in Python

丶灬走出姿态 提交于 2019-12-02 08:50:16
I'm using openpyxl to write to an existing file and everything works fine. However after the data is saved on the file, graphs disappear. I understand Openpyxl currently only supports chart creation within a worksheet only. Charts in existing workbooks will be lost. Are there any alternate libraries in Python to achieve this. I just want to feed a few values, so all the graphs and calculation happen in excel. Thank you. This is currently (version 2.2) not possible. I got some alternative solution to execute excel macro from python, that might be the solution for the above problem. Create a

Why is the iteration over this loop not adding cells in openpyxl?

二次信任 提交于 2019-12-02 07:09:29
Given the following as the contents of the first sheet of an xlsx roi.xlsx: Then: wb = load_workbook('roi.xlsx', data_only=True) ws=wb.worksheets[0] keynames = [i.value for i in ws.columns[0]] I want to add values into column B from the following dict: mydict = {'carnival': 2, 'festival': 3} When I try: for k, v in mydict.items(): keyPosition = keynames.index(k) ws.cell(row = keyPosition, column = 2).value = v I end up with a new column B, but empty values throughout, AND when I go to re-open the file after saving it with wb.save, Excel can't open the file because it is corrupted. I think the

openpyxl - Ability to remove border from charts?

自闭症网瘾萝莉.ら 提交于 2019-12-02 06:54:51
Using openpyxl, the charts inserted into my worksheet have a border on them. Is there any way to set the style of the chart (pie/bar) to either via the styles.Style/styles.borders module to have no border, or at least a thin white border so that they would print borderless? The only options I see on the object is .style = <int> which doesn't seem to actually affect the design of the final graphic. seamus.shi You can set chat graphical_properties, the code is chart.graphical_properties = GraphicalProperties(ln=LineProperties(noFill=True)) This will apply the line style to chartSpace. Before you

How to set up the Graphical Properties of Chart Area using openpyxl

℡╲_俬逩灬. 提交于 2019-12-02 04:54:53
I would like to change the background color of Chart_area as well as remove all borders. Regarding the background color, I have tried the solution suggested: How to set Background Color of Plot Area of Chart using openpyxl but this solution doesn't work for any charts i.e. BarChart, AreaChart, LineChart, ScatterChart, RadarChart etc... I tried two openpyxl versions: 2.4.7, 2.4.9. without success. # setup the chart chart = LineChart() # test to remove border line ***************************************************** chart.graphical_properties = GraphicalProperties(ln=LineProperties(noFill=True)

openpyxl change one word's color in the same cell

空扰寡人 提交于 2019-12-02 04:02:09
问题 I am using openpyxl to write some data to excel sheet. In my script, I need to append data to the same cell and highlight with different color for the new added data. Currently, I tried below but it turns out all the data's color will change at once. Is there any way to change one word's color in the same cell? from openpyxl import Workbook from openpyxl.styles import Font from openpyxl.styles import colors book = Workbook() sheet = book.active sheet.cell(row=1, column=2).value = "11111"

python: extract float from a python list of string( AUD 31.99)

走远了吗. 提交于 2019-12-02 02:15:46
问题 python: extract float from a python list of string( AUD 31.99). I used openpyxl to read from an excel file the amount list. and i saved it in a list but the list is in string form like this: ['31.40 AUD', ' 32.99 AUD', '37.24 AUD'] I need to get the float from the string item list so that i can later save it in a new list to get the total of them. Desired output: [31.40, 32.99, 37.24] I have already tried these: newList = re.findall("\d+\.\d+", tot[0]) print(newList) Output: [31.40] But How

Error in converting txt to xlsx using python

让人想犯罪 __ 提交于 2019-12-02 01:01:18
My code is following. import csv import openpyxl import sys def convert(input_path, output_path): """ Read a csv file (with no quoting), and save its contents in an excel file. """ wb = openpyxl.Workbook() ws = wb.worksheets[0] with open(input_path) as f: reader = csv.reader(f, delimiter='\t', quoting=csv.QUOTE_NONE) for row_index, row in enumerate(reader): for col_index, value in enumerate(row): ws.cell(row=row_index, column=col_index).value = value wb.save(output_path) def main(): try: input_path, output_path = sys.argv[1:] except ValueError: print 'Usage: python %s input_path output_path' %

python: extract float from a python list of string( AUD 31.99)

ぃ、小莉子 提交于 2019-12-02 01:00:12
python: extract float from a python list of string( AUD 31.99). I used openpyxl to read from an excel file the amount list. and i saved it in a list but the list is in string form like this: ['31.40 AUD', ' 32.99 AUD', '37.24 AUD'] I need to get the float from the string item list so that i can later save it in a new list to get the total of them. Desired output: [31.40, 32.99, 37.24] I have already tried these: newList = re.findall("\d+\.\d+", tot[0]) print(newList) Output: [31.40] But How can I use this for all the item elements? I am new to python, this is just for some work i do, wanted to