Openpyxl worksheet object has no attribute move_range - python

I have the below code and it errors out on move_range for some reason. I am using openpyxl version 2.5.12. I tried updating to 2.6, but then it introduced some error related to Pandas deprecated NaT feature so I'd like to stay on 2.5.12 if possible. I have included the below modules imported from openpyxl as well.
import openpyxl
from openpyxl import load_workbook
from openpyxl import Workbook
from openpyxl.utils import get_column_letter
from openpyxl.worksheet.table import Table, TableStyleInfo
from openpyxl.worksheet.cell_range import CellRange
from openpyxl.worksheet.worksheet import Worksheet
from openpyxl.worksheet import worksheet
from openpyxl import worksheet
book = load_workbook(extract_file_loc)
wb = Workbook()
ws = wb.active
book.active = 4
ws = book.active
data = gpatuple
ws.append(gp_headers_tp)
for row in data:
ws.append(row)
tab = Table(displayName="PATH_FILE", ref=final_cord)
# Add a default style with striped rows and banded columns
style = TableStyleInfo(name="TableStyleMedium2", showRowStripes=True)
tab.tableStyleInfo = style
ws.add_table(tab)
ws.move_range("G4:H10", rows=-1, cols=2)
ws.move_range(final_cord, rows=-1,cols=0)
book.save("extract.xlsx")
print "complete!"

Related

How to delete the data in a workbook from "Sheet1" and "Sheet2"?

from openpyxl import load_workbook
wb = load_workbook("C:\op.xlsx")
ws = wb.active
So this is the code I have so far. So I want to delete all the data in sheet1 and sheet2 of this workbook ("C:\op.xlsx").
Thank you in advance.

Updating excel sheet without overwriting using openpyxl :Pandas

I am trying to update sheet without overwriting the complete data but my code is creating a new sheet instead.
import csv
import openpyxl
import pandas as pd
from openpyxl import load_workbook
df1 = pd.read_csv(r'C:\Users\name\Desktop\Data_Sj.csv')
ddf = df1[
(df1['Sports'] == 'Football')
]
print(ddf)
writer = pd.ExcelWriter(r'C:\Users\name\Desktop\check\Checklist1.xlsx', engine= 'openpyxl')
book = load_workbook(r'C:\Users\name\Desktop\check\Checklist1.xlsx')
writer.book = book
ddf.to_excel(r'C:\Users\name\Desktop\check\Checklist1.xlsx')
writer.save()

OpenPyXL - Header/Footer - Can't write header to sheet

I'm trying to write a header to a spreadsheet I've created for work. I have followed the openpyxl docs as precisely as I can. However, it still won't give me a header. Here is a simple example (almost exactly as the docs) that will not work for me:
import openpyxl
wb = openpyxl.Workbook()
ws = wb.active
ws.oddHeader.text = "fffffffffffffffffff"
ws.oddHeader.size = 14
ws.oddHeader.font = "Tahoma,Bold"
ws.oddHeader.color = "CC3366"
ws.cell(row=1, column=1).value = 'hello'
wb.save('C:/Users/ffffffff/Desktop/test.xlsx')
Docs: http://openpyxl.readthedocs.io/en/default/print_settings.html
I've looked at this in more detail and the problem is in the openpyxl documentation. Headers and footers have three areas: left, centre/center and right and at least one of these must be used.
Your code only needs to be changed slightly to work:
from openpyxl import Workbook
wb = Workbook()
ws = wb.active
ws.oddHeader.center.text = "fffffffffffffffffff"
ws.oddHeader.center.size = 14
ws.oddHeader.center.font = "Tahoma,Bold"
ws.oddHeader.center.color = "CC3366"
ws.cell(row=1, column=1).value = 'hello'
wb.save('test.xlsx')

Activate second worksheet with openpyxl

I am trying to activate multiple excel worksheets and write to both multiple sheets within both workbook(s) using python and openpyxl. I am able to load the second workbook f but I am unable to append cell G2 of my second workbook with the string Recon
from openpyxl import Workbook, load_workbook
filename = 'sda_2015.xlsx'
wb = Workbook()
ws = wb.active
ws['G1'] = 'Path'
ws.title = 'Main'
adf = "Dirty Securities 04222015.xlsx"
f = "F:\\ana\\xlmacro\\" + adf
wb2 = load_workbook(f)
"""
wb22 = Workbook(wb2)
ws = wb22.active
ws['G1'] = "Recon2"
ws.title = 'Main2'
"""
print wb2.get_sheet_names()
wb.save(filename)
I commented out the code which is broken
Update
I adjusted my code with the below answer. The value in cell H1 is written onto wb2 in column H, but for some reason the column is hidden. I have adjusted the column to other columns but still I have seen the code hide multiple columns. There are also occurences when the code executes and titles ws2 as Main21 but the encoded value is Main2
from openpyxl import Workbook, load_workbook
filename = 'sda_2015.xlsx'
wb1 = Workbook()
ws1 = wb1.active
ws1['G1'] = 'Path'
ws1.title = 'Main'
adf = "Dirty Securities 04222015.xlsx"
f = "F:\\ana\\xlmacro\\" + adf
wb2 = load_workbook(f)
ws2 = wb2.active
ws2['H1'] = 'Recon2'
ws2.title = 'Main2'
print wb2.get_sheet_names()
wb1.save(filename)
wb2.save(f)
If you have two workbooks open, wb1 and wb2, you'll also need different names for the various worksheets: ws1 = wb1.active and ws2 = wb2.active.
If you're working with a file with macros, you'll need to set the keep_vba flag to True when opening it in order to preserve the macros.
I had experienced the same thing with hidden cells. Eventually, I unpacked the Excel file and looked at the raw XML to find out that not all of the columns had a dimension for width. Those without a width were being by Excel.
A quick fix is to do something like this...
for col in 'ABCDEFG':
if not worksheet.column_dimensions[col].width:
worksheet.column_dimensions[col].width = 10

Coloring a tab in openpyxl

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 color the tabs with openpyxl by using RRGGBB color code for sheet_properties.tabColor property:
from openpyxl import Workbook
wb = Workbook()
ws = wb.create_sheet('My_Color_Title')
ws.sheet_properties.tabColor = 'FFFF00'
wb.save('My_book_with_Yellow_Tab.xlsx')
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()
# Set tab colours
worksheet1.set_tab_color('red')
worksheet2.set_tab_color('green')
worksheet3.set_tab_color('#FF9900') # Orange
# worksheet4 will have the default colour.
workbook.close()

Categories