openpyxl

Modify an existing Excel file using Openpyxl in Python

家住魔仙堡 提交于 2019-11-27 12:14:01
I am basically trying to copy some specific columns from a CSV file and paste those in an existing excel file[*.xlsx] using python. Say for example, you have a CSV file like this : col_1 col_2 col_3 col_4 1 2 3 4 5 6 7 8 9 10 11 12 So, i wanted to copy the both col_3 and col_4 and paste those in col_8 and col_9 in an existing excel file [which is a .XLSX format]. I have tried this in various way to solve, but could not find out the exact way. i tried something like this : with open( read_x_csv, 'rb') as f: reader = csv.reader(f) for row in reader: list1 = row[13] queue1.append(list1) list2 =

Setting styles in Openpyxl

∥☆過路亽.° 提交于 2019-11-27 11:46:36
I need advice on setting styles in Openpyxl. I see that the NumberFormat of a cell can be set, but I also require setting of font colors and attributes (bold etc). There is a style.py class but it seems I can't set the style attribute of a cell, and I don't really want to start tinkering with the openpyxl source code. Has anyone found a solution to this? As of openpyxl version 1.5.7, I have successfully applied the following worksheet style options... from openpyxl.reader.excel import load_workbook from openpyxl.workbook import Workbook from openpyxl.styles import Color, Fill from openpyxl

How to save XLSM file with Macro, using openpyxl

对着背影说爱祢 提交于 2019-11-27 09:41:31
I have .xlsm file with a Macro function. I'm loading it using openpyxl and write some data to the file and finally want to save as a different .xlsm file. To save the file as XLSM file I have used below code in my Python script. wb.save('testsave.xlsm'); But I cannot open that file if I saved as above. But if I saved it as .xlsx then I can open the file without the Macro function that original file had. I want to open a Excel sheet that has Macro function, edit the file and save it as new .xlsm file using openpyxl . How can I do that? For me this worked, together with version openpyxl==2.3.0

python之openpyxl生成excel文件

≯℡__Kan透↙ 提交于 2019-11-27 09:39:37
   项目需要,需要自动生成PDF测试报告。经过对比之后,选择使用了reportlab模块。 项目背景:开发一个测试平台,供测试维护测试用例,执行测试用例,并且生成测试报告(包含PDF和excel),将生成的测试报告以邮件的形式发送相关人。    excel生成代码如下: from openpyxl import load_workbook from openpyxl.styles import Font, Alignment, Side, Border import shutil # 生成测试计划的excel文件 class GenerateCaseExcel(object): def __init__(self, file_name): self.file_name = file_name self.file_path = '/xxx/xxx/xxx/' self.font_title = Font(name=u"宋体", size=12, bold=True) self.font_body = Font(name=u"宋体", size=10) self.alignment_center = Alignment(horizontal='center', vertical='center', wrap_text=True) self.alignment_left =

Is there a way to auto-adjust Excel column widths with pandas.ExcelWriter?

情到浓时终转凉″ 提交于 2019-11-27 09:19:38
问题 I am being asked to generate some Excel reports. I am currently using pandas quite heavily for my data, so naturally I would like to use the pandas.ExcelWriter method to generate these reports. However the fixed column widths are a problem. The code I have so far is simple enough. Say I have a dataframe called 'df': writer = pd.ExcelWriter(excel_file_path) df.to_excel(writer, sheet_name="Summary") I was looking over the pandas code, and I don't really see any options to set column widths. Is

Copy whole worksheet with openpyxl

时光总嘲笑我的痴心妄想 提交于 2019-11-27 08:57:26
Please can someone give me an example, how to copy a whole worksheet with styles (from rows and columns) to a second worksheet in the same workbook ? (in a new workbook would also be possible) Thank you. P.S.: I tried to do a deepcopy, but that failed on saving changed data cells. Purpose is: I try to fill some worksheets with my data and the first worksheet is my template. I was successful in copying the values but only some styles . I am using the latest version of openpyxl, so please no 1.x methods. psychok7 Version 2.4 will allow you to do this: copy_worksheet >>> source = wb.active >>>

Iterate over Worksheets, Rows, Columns

左心房为你撑大大i 提交于 2019-11-27 08:30:19
I want to print all data (all rows) of a specific column in python using openpyxl I am working in this way; from openpyxl import load_workbook workbook = load_workbook('----------/dataset.xlsx') sheet = workbook.active for i in sheet: print(sheet.cell(row=i, column=2).value) But it gives if row < 1 or column < 1: TypeError: unorderable types: tuple() < int() Because i am iterating in row=i . If I use sheet.cell(row=4, column=2).value it print the value of cell. But how can I iterate over all document? Edit 1 On some research, it is found that data can be get using Sheet Name. The Sheet 1

Openpyxl-read formulas results (Python 2.7)

℡╲_俬逩灬. 提交于 2019-11-27 08:24:01
问题 I am using openpyxl to read from an excel file. I am trying to read a cell whose value is calculated by a formula. Regular reading functions returns formula script: `wb= openpyxl.load_workbook('forecast.xlsx')` `sheet = wb.get_sheet_by_name('Sheet3')` `result=sheet["F6"].value` I tried to use (data_only=True) flag like this: wb= openpyxl.load_workbook('forecast.xlsx', data_only=True) Result was that all formula cells turned to blanks. Only pure values remained. Is there a way to read a cell

python Search a string in a column, and return another column value from that same row

我的未来我决定 提交于 2019-11-27 08:06:52
问题 I am currently trying to write a script that will evaluate a spreadsheet to search column A for a string, and then once it finds that string, print the value of column I in the same row. So far I have the following code but it isn't getting me very far. any help will be appreciated. import openpyxl wb = openpyxl.load_workbook("2016_SF.xlsx", data_only=True) ws = wb["161226-161228"] last_r=70 search_str = raw_input("What plant are you looking for? > ") last_r = cell.row def FindXlCell(search

Insert row into Excel spreadsheet using openpyxl in Python

自闭症网瘾萝莉.ら 提交于 2019-11-27 05:16:46
I'm looking for the best approach for inserting a row into a spreadsheet using openpyxl. Effectively, I have a spreadsheet (Excel 2007) which has a header row, followed by (at most) a few thousand rows of data. I'm looking to insert the row as the first row of actual data, so after the header. My understanding is that the append function is suitable for adding content to the end of the file. Reading the documentation for both openpyxl and xlrd (and xlwt), I can't find any clear cut ways of doing this, beyond looping through the content manually and inserting into a new sheet (after inserting