openpyxl

openpyxl merged cells: Formatting issue

早过忘川 提交于 2019-12-05 12:59:10
Say I have an xlsx-file and this Excel file has cells A2 and B2 merged. I also select that merged cell and put a border at the top and bottom of the merged cell. When I do the following: wb = openpyxl.load_workbook("file.xlsx") wb.save("resulting file.xlsx") The result is that only the portion around the merged cell that is in column A now has the border: Is there a way to prevent this from happening? I believe you are facing Issue#365 , there is generally an issue around styling for merged cells, I believe the above mentioned issues tracks it. Sadly, I do not see any workaround for this issue

Python自动化办公之操作Excel文件

旧时模样 提交于 2019-12-05 12:08:31
模块导入 import openpyxl 读取Excel文件 打开Excel文件 workbook = openpyxl.load_workbook("test.xlsx") 输出表单名字 # 输出工作簿中所有表单名字 print(workbook.sheetnames) # 遍历所有表单并输出其名字 for sheet in workbook: print(sheet.title) 创建表单 newSheet = workbook.create_sheet("newSheetName") 获取表单对象 # 根据表单名获取表单 sheet3 = workbook.get_sheet_by_name("Sheet3") sheet4 = workbook["newSheetName"] # 获取当前活跃的表单 worksheet = workbook.active 获取当前表单数据行列数 # 获取当前表单数据行数 row_count = worksheet.max_row # 获取当前表单数据列数 row_count = worksheet.max_column 获取单元格对象 selectcell = worksheet["A1"] selectcell = worksheet.cell(row=1, column=2) # 行列号从1开始 输出单元格信息 单元格所在的行、列

openpyxl font conditional formatting

£可爱£侵袭症+ 提交于 2019-12-05 11:25:49
I am using pyopenxl to output some excel spreadsheets, I encountered a problem with font conditional formatting. I want to highlight the cells lesser than 0 with red color and here's what I've done: from pyopenxl import formatting, styles red_font = styles.Font(size=self.font_size, bold=bold, color=self.red_color_font) red_fill = styles.PatternFill(start_color=self.red_color, end_color=self.red_color, fill_type='solid') self.ws.conditional_formatting.add( cell.coordinate, formatting.CellIsRule(operator='lessThan', formula=['0'], fill=red_fill, font=red_font) ) So I simply created styles for

How do I add a column to an existing excel file using python?

怎甘沉沦 提交于 2019-12-05 11:23:52
here is my code: import openpyxl, pprint wb = openpyxl.load_workbook('/Users/sarahporgess/Desktop/SSA1.xlsx') sheet = wb.get_sheet_by_name('Sheet1') data = {} for row in range(1,sheet.max_row+1): date = sheet['A' +str(row)].value gamma = sheet['B' +str(row)].value theta = sheet['C' +str(row)].value ratio = float(gamma)/float(theta) resultFile = open('SSA2.csv' , 'w') resultFile.write( pprint.pformat(date)) resultFile.write( pprint.pformat(gamma)) resultFile.write( pprint.pformat(theta)) resultFile.write( pprint.pformat(ratio)) print(ratio) sheet['D1']=ratio resultFile.close() print('Done.') my

Openpyxl auto-height row

时光毁灭记忆、已成空白 提交于 2019-12-05 07:14:27
I'm trying to set wrap text. But when i using wrap text row doesn't change height automatically. How can I set auto-height row? Charlie Clark You need to look at the RowDimension object for the relevant row, specifically the height attribute: rd = ws.row_dimensions[3] # get dimension for row 3 rd.height = 25 # value in points, there is no "auto" If your data stored in DataFrame, I would recommend you to use StyleFrame. It automatically auto-adjust columns width and rows height and also have some nice features. styleframe Try: col_width = [] for i in range(len(next(ws.iter_rows()))): col_letter

How do i get value present in a merged cell

假如想象 提交于 2019-12-05 07:03:53
I want to get value of a merged cell that has range from D3 to H3 using openpyxl library. As per my understanding most libraries read data from 1st cell itself. Thus the merged content is present in it but I get a none value when I read it. Following is my code: wb = load_workbook(work_directory_path+'/outputs/report_vap.xlsx') ws = wb.get_sheet_by_name("Summary") suite_path = ws.cell('D3').value if not isinstance(suite_path, unicode): value=unicode(suite_path) value=value.encode('utf8') print "Suite Location is "+value; The output is : Suite Location is None The value in cell for D3 to H3 is

Coloring a tab in openpyxl

末鹿安然 提交于 2019-12-05 06:38:40
We have a situation where we want to color the tabs for the worksheets using openpyxl. Is there a way to do this within the library? Or, has anyone found a way to do this external to the library (i.e. by extension or something similar)? You can set the tab color in a new Excel file using the XlsxWriter Python module. Here is an example: from xlsxwriter.workbook import Workbook workbook = Workbook('tab_colors.xlsx') # Set up some worksheets. worksheet1 = workbook.add_worksheet() worksheet2 = workbook.add_worksheet() worksheet3 = workbook.add_worksheet() worksheet4 = workbook.add_worksheet() #

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

How to create a hyperlink to a different Excel sheet in the same workbook

旧街凉风 提交于 2019-12-05 05:00:32
I'm using the module openpyxl for Python and am trying to create a hyperlink that will take me to a different tab in the same Excel workbook. Doing something similar to the following creates the hyperlink; however, when I click on it, it tells me it can't open the file. from openpyxl import Workbook wb = Workbook() first_sheet = wb.create_sheet(title='first') second_sheet = wb.create_sheet(title='second') first_sheet['A1'] = "hello" second_sheet['B2'] = "goodbye" link_from = first_sheet['A1'] link_to = second_sheet['B2'].value link_from.hyperlink = link_to wb.save("C:/somepath/workbook.xlsx")

python操作Excel模块openpyxl

风格不统一 提交于 2019-12-05 04:38:14
1、 安装 pip install openpyxl 想要在文件中插入图片文件,需要安装pillow,安装文件:PIL-fork-1.1.7.win-amd64-py2.7.exe · font(字体类):字号、字体颜色、下划线等 · fill(填充类):颜色等 · border(边框类):设置单元格边框 · alignment(位置类):对齐方式 · number_format(格式类):数据格式 · protection(保护类):写保护 2、 创建 一个 excel 文件 ,并 写入 不同类的内容 # -*- coding: utf-8 -*- from openpyxl import Workbook wb = Workbook() #创建文件对象 # grab the active worksheet ws = wb.active #获取第一个sheet # Data can be assigned directly to cells ws['A1'] = 42 #写入数字 ws['B1'] = "你好"+"automation test" #写入中文(unicode中文也可) # Rows can also be appended ws.append([1, 2, 3]) #写入多个单元格 # Python types will automatically be