質問編集履歴

11

.

2020/04/24 10:57

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -29,6 +29,8 @@
29
29
  excel.Quit()
30
30
 
31
31
  の2つをfor文の外に出しました。
32
+
33
+ それでもエクセルが開いたままになっています。
32
34
 
33
35
 
34
36
 

10

.

2020/04/24 10:57

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -32,9 +32,9 @@
32
32
 
33
33
 
34
34
 
35
- # pdfの作成、保存
35
+ pdfの作成、保存
36
36
 
37
- 下の行を消すとエクセルはしっかりと終了
37
+ より下の行を消すとエクセルはしっかりと終了
38
38
 
39
39
  してくれます。
40
40
 

9

.

2020/04/24 10:25

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -20,11 +20,37 @@
20
20
 
21
21
 
22
22
 
23
+ ### 試したこと
24
+
25
+ 修正依頼を受けて
26
+
27
+ excel = win32com.client.DispatchEx("Excel.Application")
28
+
29
+ excel.Quit()
30
+
31
+ の2つをfor文の外に出しました。
32
+
33
+
34
+
35
+ # pdfの作成、保存
36
+
37
+ 以下の行を消すとエクセルはしっかりと終了
38
+
39
+ してくれます。
40
+
41
+
42
+
23
43
  ```python
44
+
45
+ import openpyxl, pprint
24
46
 
25
47
  import datetime
26
48
 
27
49
  import locale
50
+
51
+ import calendar
52
+
53
+ from dateutil.relativedelta import relativedelta
28
54
 
29
55
  import win32com.client #win32comをインポートするだけでは上手くいかないので注意!!
30
56
 
@@ -36,33 +62,113 @@
36
62
 
37
63
  # ファイル名の指定など --- (*1)
38
64
 
39
- # file_list = r"G:\Fのバックアップ\ドキュメント\銀行明細関連\絵本-invoice\atena.xlsx"
65
+ file_list = r"G:\Fのバックアップ\ドキュメント\銀行明細関連\primefee-invoice\atena.xlsx"
40
66
 
41
- # file_invoice= r"G:\Fのバックアップ\ドキュメント\銀行明細関連\絵本-invoice\hinagata.xlsx"
67
+ file_invoice= r"G:\Fのバックアップ\ドキュメント\銀行明細関連\primefee-invoice\hinagata.xlsx"
42
68
 
43
- # file_out_iv = r"G:\Fのバックアップ\ドキュメント\銀行明細関連\絵本-invoice\invoices\out-invoice.xlsx"
69
+ file_out_iv = r"G:\Fのバックアップ\ドキュメント\銀行明細関連\primefee-invoice\invoices\out-invoice.xlsx"
70
+
71
+
72
+
73
+
74
+
75
+ # 宛名一覧を読み込む --- (*2)
76
+
77
+ wb = openpyxl.load_workbook(file_list, data_only=True) # 数式でなく値を取り出す場合
78
+
79
+ ws = wb["Sheet1"] # Sheet1を選ぶ
80
+
81
+ # 起点となる日
82
+
83
+
84
+
85
+ excel = win32com.client.DispatchEx("Excel.Application")
44
86
 
45
87
 
46
88
 
47
89
  for i in range(2,8):
48
90
 
91
+ name = ws.cell(row = i, column=1).value # 宛名を得る
49
92
 
93
+
94
+
95
+ date = datetime.datetime(2020,7,1)
96
+
97
+ print('起点となる日:'+date.strftime('%Y年%m月%d日'))
98
+
99
+
100
+
101
+ shimemo = date + relativedelta(months=2)
102
+
103
+
104
+
105
+ shimebi = calendar.monthrange(int(shimemo.strftime('%Y')),int(shimemo.strftime('%m')))[1]
106
+
107
+
108
+
109
+ # 明細の作成
110
+
111
+ meisai = 'primefee.com利用料'+date.strftime('%Y年%m月%d日')+'~'+shimemo.strftime('%Y年%m月')+str(shimebi)+'日'
112
+
113
+
114
+
115
+ # list_data = ws["A3:F10"] # 任意の範囲を取得
116
+
117
+
118
+
119
+ # 請求書のテンプレートを読む --- (*3)
120
+
121
+ wb_iv = openpyxl.load_workbook(file_invoice)
122
+
123
+ ws_iv = wb_iv.active
124
+
125
+
126
+
127
+ # wb_ds = openpyxl.load_workbook(file_delivery)
128
+
129
+ #ws_ds = wb_ds.active
130
+
131
+
132
+
133
+ # 宛名と日付を書き込む --- (*4)
134
+
135
+ cdate = datetime.datetime.today().strftime("%Y/%m/%d")
136
+
137
+ ws_iv["C6"].value = name
138
+
139
+ ws_iv["F2"].value = cdate
140
+
141
+ ws_iv["C13"].value = meisai
142
+
143
+
144
+
145
+ # ws_ds["A3"].value = name
146
+
147
+ # ws_ds["F2"].value = cdate
148
+
149
+
150
+
151
+ # 新しく保存する --- (*6)
152
+
153
+ wb_iv.save(r'G:\Fのバックアップ\ドキュメント\銀行明細関連\primefee-invoice\invoices'+ '\' + name + 'primefee請求書' + date.strftime('%Y年%m月%d日') + '.xlsx')
154
+
155
+ wb_iv.close()
156
+
157
+
50
158
 
51
159
  # pdfの作成、保存
52
160
 
53
- excel = win32com.client.DispatchEx("Excel.Application")
54
161
 
162
+
55
- file = excel.Workbooks.Open(r'G:\Fのバックアップ\ドキュメント\銀行明細関連\絵本-invoice\invoices'+ '\' + name + '絵本請求書' + date.strftime('%Y年%m月%d日') + '.xlsx')
163
+ file = excel.Workbooks.Open(r'G:\Fのバックアップ\ドキュメント\銀行明細関連\primefee-invoice\invoices'+ '\' + name + 'primefee請求書' + date.strftime('%Y年%m月%d日') + '.xlsx')
56
164
 
57
165
  file.WorkSheets(1).Select()
58
166
 
59
- file.ActiveSheet.ExportAsFixedFormat(0,r'G:\Fのバックアップ\ドキュメント\銀行明細関連\絵本-invoice\invoices'+ '\' + name + '絵本請求書' + date.strftime('%Y年%m月%d日') + '.pdf')
167
+ file.ActiveSheet.ExportAsFixedFormat(0,r'G:\Fのバックアップ\ドキュメント\銀行明細関連\primefee-invoice\invoices'+ '\' + name + 'primefee請求書' + date.strftime('%Y年%m月%d日') + '.pdf')
60
168
 
61
169
  file.Close()
62
170
 
63
- excel.Quit()
171
+ excel.Quit()
64
-
65
- print("ok:{}".format(name))
66
172
 
67
173
  ```
68
174
 

8

COMタグを入れた

2020/04/24 10:24

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
File without changes

7

追記

2020/04/24 09:24

投稿

退会済みユーザー
test CHANGED
@@ -1 +1 @@
1
- Pythonのopenpyxlで複数のエクセルを開いてPDF出力後、裏でエクセルが起動したままになってしまう。
1
+ Pythonで複数のエクセルをPDF出力後、裏でエクセルが起動したままになってしまう。
test CHANGED
@@ -22,15 +22,9 @@
22
22
 
23
23
  ```python
24
24
 
25
- import openpyxl, pprint
26
-
27
25
  import datetime
28
26
 
29
27
  import locale
30
-
31
- import calendar
32
-
33
- from dateutil.relativedelta import relativedelta
34
28
 
35
29
  import win32com.client #win32comをインポートするだけでは上手くいかないので注意!!
36
30
 
@@ -42,11 +36,11 @@
42
36
 
43
37
  # ファイル名の指定など --- (*1)
44
38
 
45
- file_list = r"G:\Fのバックアップ\ドキュメント\銀行明細関連\絵本-invoice\atena.xlsx"
39
+ # file_list = r"G:\Fのバックアップ\ドキュメント\銀行明細関連\絵本-invoice\atena.xlsx"
46
40
 
47
- file_invoice= r"G:\Fのバックアップ\ドキュメント\銀行明細関連\絵本-invoice\hinagata.xlsx"
41
+ # file_invoice= r"G:\Fのバックアップ\ドキュメント\銀行明細関連\絵本-invoice\hinagata.xlsx"
48
42
 
49
- file_out_iv = r"G:\Fのバックアップ\ドキュメント\銀行明細関連\絵本-invoice\invoices\out-invoice.xlsx"
43
+ # file_out_iv = r"G:\Fのバックアップ\ドキュメント\銀行明細関連\絵本-invoice\invoices\out-invoice.xlsx"
50
44
 
51
45
 
52
46
 

6

:

2020/04/24 09:12

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -50,22 +50,6 @@
50
50
 
51
51
 
52
52
 
53
-
54
-
55
- #請求書の作成
56
-
57
- # 宛名一覧を読み込む
58
-
59
- wb = openpyxl.load_workbook(file_list, data_only=True) # 数式でなく値を取り出す場合
60
-
61
- ws = wb["Sheet1"] # Sheet1を選ぶ
62
-
63
- # 起点となる日
64
-
65
-
66
-
67
-
68
-
69
53
  for i in range(2,8):
70
54
 
71
55
 

5

:

2020/04/24 08:47

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -1,4 +1,6 @@
1
- エクセルで複数の請求書を作成して、PDFに全て変換して出力するプログラムを書いています。
1
+ エクセルで複数の請求書を作成した後にPDFに全て変換して出力するプログラムを書いています。
2
+
3
+
2
4
 
3
5
 
4
6
 
@@ -8,11 +10,9 @@
8
10
 
9
11
 
10
12
 
11
- ですので、該当のエクセルファイルを開こうとすると「読み取り専用で開きますか?」と聞かれます。
12
13
 
13
14
 
14
-
15
- プログラムの終了時にエクセルを閉じたいです。
15
+ プログラムの終了時にエクセルを閉じたいですが、調べても間違っているようなところは特にないように思います
16
16
 
17
17
 
18
18
 
@@ -50,6 +50,8 @@
50
50
 
51
51
 
52
52
 
53
+
54
+
53
55
  #請求書の作成
54
56
 
55
57
  # 宛名一覧を読み込む
@@ -62,73 +64,9 @@
62
64
 
63
65
 
64
66
 
65
- for i in range(2,8):
66
-
67
- name = ws.cell(row = i, column=1).value # 宛名を得る
68
67
 
69
68
 
70
-
71
- date = datetime.datetime(2020,7,1)
72
-
73
- print('起点となる日:'+date.strftime('%Y年%m月%d日'))
74
-
75
-
76
-
77
- shimemo = date + relativedelta(months=2)
78
-
79
-
80
-
81
- shimebi = calendar.monthrange(int(shimemo.strftime('%Y')),int(shimemo.strftime('%m')))[1]
82
-
83
-
84
-
85
- # 明細の作成
86
-
87
- meisai = '絵本.com利用料'+date.strftime('%Y年%m月%d日')+'~'+shimemo.strftime('%Y年%m月')+str(shimebi)+'日'
88
-
89
-
90
-
91
- # list_data = ws["A3:F10"] # 任意の範囲を取得
92
-
93
-
94
-
95
- # 請求書のテンプレートを読む
96
-
97
- wb_iv = openpyxl.load_workbook(file_invoice)
98
-
99
- ws_iv = wb_iv.active
69
+ for i in range(2,8):
100
-
101
-
102
-
103
- # wb_ds = openpyxl.load_workbook(file_delivery)
104
-
105
- #ws_ds = wb_ds.active
106
-
107
-
108
-
109
- # 宛名と日付を書き込む
110
-
111
- cdate = datetime.datetime.today().strftime("%Y/%m/%d")
112
-
113
- ws_iv["C6"].value = name
114
-
115
- ws_iv["F2"].value = cdate
116
-
117
- ws_iv["C13"].value = meisai
118
-
119
-
120
-
121
- # ws_ds["A3"].value = name
122
-
123
- # ws_ds["F2"].value = cdate
124
-
125
-
126
-
127
- # 新しく保存する
128
-
129
- wb_iv.save(r'G:\Fのバックアップ\ドキュメント\銀行明細関連\絵本-invoice\invoices'+ '\' + name + '絵本請求書' + date.strftime('%Y年%m月%d日') + '.xlsx')
130
-
131
- wb_iv.close()
132
70
 
133
71
 
134
72
 
@@ -142,7 +80,7 @@
142
80
 
143
81
  file.ActiveSheet.ExportAsFixedFormat(0,r'G:\Fのバックアップ\ドキュメント\銀行明細関連\絵本-invoice\invoices'+ '\' + name + '絵本請求書' + date.strftime('%Y年%m月%d日') + '.pdf')
144
82
 
145
- # file.Close()
83
+ file.Close()
146
84
 
147
85
  excel.Quit()
148
86
 

4

追記

2020/04/24 08:46

投稿

退会済みユーザー
test CHANGED
@@ -1 +1 @@
1
- Pythonのopenpyxlでエクセルが閉じ
1
+ Pythonのopenpyxlで複数のエクセルを開いてPDF出力後、裏でエクセル起動したままにってしまう。
test CHANGED
@@ -50,7 +50,7 @@
50
50
 
51
51
 
52
52
 
53
-
53
+ #請求書の作成
54
54
 
55
55
  # 宛名一覧を読み込む
56
56
 

3

:

2020/04/24 08:12

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -42,11 +42,11 @@
42
42
 
43
43
  # ファイル名の指定など --- (*1)
44
44
 
45
- file_list = r"G:\Fのバックアップ\ドキュメント\銀行明細関連\ahrefs-invoice\atena.xlsx"
45
+ file_list = r"G:\Fのバックアップ\ドキュメント\銀行明細関連\絵本-invoice\atena.xlsx"
46
46
 
47
- file_invoice= r"G:\Fのバックアップ\ドキュメント\銀行明細関連\ahrefs-invoice\hinagata.xlsx"
47
+ file_invoice= r"G:\Fのバックアップ\ドキュメント\銀行明細関連\絵本-invoice\hinagata.xlsx"
48
48
 
49
- file_out_iv = r"G:\Fのバックアップ\ドキュメント\銀行明細関連\ahrefs-invoice\invoices\out-invoice.xlsx"
49
+ file_out_iv = r"G:\Fのバックアップ\ドキュメント\銀行明細関連\絵本-invoice\invoices\out-invoice.xlsx"
50
50
 
51
51
 
52
52
 
@@ -66,7 +66,7 @@
66
66
 
67
67
  name = ws.cell(row = i, column=1).value # 宛名を得る
68
68
 
69
-
69
+
70
70
 
71
71
  date = datetime.datetime(2020,7,1)
72
72
 
@@ -84,7 +84,7 @@
84
84
 
85
85
  # 明細の作成
86
86
 
87
- meisai = 'ahrefs.com利用料'+date.strftime('%Y年%m月%d日')+'~'+shimemo.strftime('%Y年%m月')+str(shimebi)+'日'
87
+ meisai = '絵本.com利用料'+date.strftime('%Y年%m月%d日')+'~'+shimemo.strftime('%Y年%m月')+str(shimebi)+'日'
88
88
 
89
89
 
90
90
 
@@ -126,21 +126,21 @@
126
126
 
127
127
  # 新しく保存する
128
128
 
129
- wb_iv.save(r'G:\Fのバックアップ\ドキュメント\銀行明細関連\ahrefs-invoice\invoices'+ '\' + name + 'ahrefs請求書' + date.strftime('%Y年%m月%d日') + '.xlsx')
129
+ wb_iv.save(r'G:\Fのバックアップ\ドキュメント\銀行明細関連\絵本-invoice\invoices'+ '\' + name + '絵本請求書' + date.strftime('%Y年%m月%d日') + '.xlsx')
130
130
 
131
131
  wb_iv.close()
132
132
 
133
-
133
+
134
134
 
135
135
  # pdfの作成、保存
136
136
 
137
137
  excel = win32com.client.DispatchEx("Excel.Application")
138
138
 
139
- file = excel.Workbooks.Open(r'G:\Fのバックアップ\ドキュメント\銀行明細関連\ahrefs-invoice\invoices'+ '\' + name + 'ahrefs請求書' + date.strftime('%Y年%m月%d日') + '.xlsx')
139
+ file = excel.Workbooks.Open(r'G:\Fのバックアップ\ドキュメント\銀行明細関連\絵本-invoice\invoices'+ '\' + name + '絵本請求書' + date.strftime('%Y年%m月%d日') + '.xlsx')
140
140
 
141
141
  file.WorkSheets(1).Select()
142
142
 
143
- file.ActiveSheet.ExportAsFixedFormat(0,r'G:\Fのバックアップ\ドキュメント\銀行明細関連\ahrefs-invoice\invoices'+ '\' + name + 'ahrefs請求書' + date.strftime('%Y年%m月%d日') + '.pdf')
143
+ file.ActiveSheet.ExportAsFixedFormat(0,r'G:\Fのバックアップ\ドキュメント\銀行明細関連\絵本-invoice\invoices'+ '\' + name + '絵本請求書' + date.strftime('%Y年%m月%d日') + '.pdf')
144
144
 
145
145
  # file.Close()
146
146
 

2

*

2020/04/24 07:39

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -150,12 +150,14 @@
150
150
 
151
151
  ```
152
152
 
153
- 環境
153
+ ### 環境
154
154
 
155
155
 
156
156
 
157
157
  Windows10
158
158
 
159
+ python3.6.7
160
+
159
161
 
160
162
 
161
163
  どのようにすればプログラムの最後にエクセルを閉じることができるでしょうか?

1

追記

2020/04/24 07:34

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -5,6 +5,14 @@
5
5
  プログラムは最後まで実行できるのですが、なぜかタスクマネージャーを見るとエクセルが閉じられてません。
6
6
 
7
7
  ただ、実際にはエクセルは起動しているようには見えないです。(裏でエクセルが開いたままになっちゃってます。)
8
+
9
+
10
+
11
+ ですので、該当のエクセルファイルを開こうとすると「読み取り専用で開きますか?」と聞かれます。
12
+
13
+
14
+
15
+ プログラムの終了時にエクセルを閉じたいです。
8
16
 
9
17
 
10
18